Page 1 of 3

Grid Sort (Function / Method)

Posted: Fri Jul 01, 2016 12:38 pm
by Roberto Lopez
Hi All,

Another little thing I've added to my personal library to easy my work: It is a grid sort utility.

I've created as a function but could be added to DoMethod().

SortGrid Function:

Code: Select all

*----------------------------------------------------------------------*
PROCEDURE SortGrid( cWindow , cGrid , nColumn , cSortAs )
*----------------------------------------------------------------------*
LOCAL L
LOCAL I
LOCAL aGrid := {}
LOCAL J
LOCAL TEMP

	WAIT WINDOW 'Sorting...' NOWAIT

	* nColumn Parameter is Optional. Default is 1

	IF VALTYPE(nColumn) = 'U'
		nColumn := 1
	ENDIF

	* cSrotAs parameter is optional. Default is 'C'

	IF VALTYPE(cSortAs) = 'U'
		cSortAs := 'C'
	ENDIF

	* Copy the grid content to an array.

	L := GetProperty ( cWindow , cGrid , 'ItemCount' )

	FOR I := 1 TO L
		AADD( aGrid , GetProperty( cWindow , cGrid , 'Item' , I ) )
	NEXT I

	* Then Sort The Array...

	IF	ALLTRIM(UPPER(cSortAs)) == 'C' // Sort As Character

		FOR i=1 TO L
			FOR j=1 TO (L-1)
				IF aGrid[j,nColumn] > aGrid[j+1,nColumn]
					temp = aGrid[j]
					aGrid[j]=aGrid[j+1]
					aGrid[j+1]=temp
				ENDIF
			NEXT j
		NEXT i

	ELSEIF	ALLTRIM(UPPER(cSortAs)) == 'N' // Sort As Number

		FOR i=1 TO L
			FOR j=1 TO (L-1)
				IF VAL(aGrid[j,nColumn]) > VAL(aGrid[j+1,nColumn])
					temp = aGrid[j]
					aGrid[j]=aGrid[j+1]
					aGrid[j+1]=temp
				ENDIF
			NEXT j
		NEXT i

	ELSEIF	ALLTRIM(UPPER(cSortAs)) == 'D' // Sort As Date

		FOR i=1 TO L
			FOR j=1 TO (L-1)
				IF CTOD(aGrid[j,nColumn]) > CTOD(aGrid[j+1,nColumn])
					temp = aGrid[j]
					aGrid[j]=aGrid[j+1]
					aGrid[j+1]=temp
				ENDIF
			NEXT j
		NEXT i

	ENDIF

	* Finally, Fill The Grid With The Sorted Array Content...

	DoMethod( cWindow , cGrid , 'DeleteAllItems' )
	
	FOR I := 1 TO L
		DoMethod( cWindow , cGrid , 'AddItem' , aGrid[I] )
	NEXT I

	* End
	
	WAIT CLEAR

RETURN
To use as a Grid method:

Since the 'nColumn' and 'cSortAs' parameters are optional, it should be handled differently according the parameter count:

Code: Select all

<...>
// Pcount() == 3 // CONTROL

    elseIf     Arg3 == 'SORT'

        SortGrid ( Arg1 , Arg2  )
<...>
// Pcount() == 4    // CONTROL WITH 1 ARGUMENT
<...>
    ElseIf Arg3 == 'SORT'

        SortGrid ( Arg1 , Arg2 , Arg4 )
<...>
// Pcount() == 5 .And. ValType (Arg3) == 'C' // CONTROL WITH 2 ARGUMENTS
<...>
    ElseIf Arg3 == 'SORT'

        SortGrid ( Arg2 , Arg1 , Arg4 , Arg5 ) 
<...>
I hope this be useful for someone.

Re: Grid Sort (Function / Method)

Posted: Fri Jul 01, 2016 1:33 pm
by srvet_claudio
Very nice, you used "burbuja" as sort method?
I will adapted for Unicode.

Re: Grid Sort (Function / Method)

Posted: Fri Jul 01, 2016 2:03 pm
by Roberto Lopez
srvet_claudio wrote:Very nice, you used "burbuja" as sort method?
Oh Yeah... I like bubble sort!

But if you consider that another method could be more efficient... no problem from my part.
srvet_claudio wrote: I will adapted for Unicode.
Thanks!

Re: Grid Sort (Function / Method)

Posted: Fri Jul 01, 2016 3:27 pm
by Rathinagiri
My two cents:

1. We can use aSort() function
2. Before we start we can use DisableUpdate and after we finish, use EnableUpdate.
3. Add another parameter to allow ascending/descending order.

What would be the position for a virtually managed grid?

Re: Grid Sort (Function / Method)

Posted: Fri Jul 01, 2016 4:19 pm
by serge_girard
Hi,

I use ON HEADCLICK to sort a grid on a specific field. Like this:

Code: Select all

   ON HEADCLICK { { || Sort_Grid_Tree(1)} , {|| Sort_Grid_Tree(2)}  , { || Sort_Grid_Tree(3)}, { || Sort_Grid_Tree(4)} , { || Sort_Grid_Tree(5)} , {|| Sort_Grid_Tree(6)},  { || Sort_Grid_Tree(7)}  ,  { || Sort_Grid_Tree(8)} ,  { || Sort_Grid_Tree(9)},  { || Sort_Grid_Tree(10)} ,  { || Sort_Grid_Tree(11)}  } ;
So for every field the sort function is called. In the SORT_GRID_TREE some fields need to be sorted as numerics (but displayed as chars).
This is the most unconvenience I found. But overall it works great.

But a default sort would be great!

Serge

Re: Grid Sort (Function / Method)

Posted: Fri Jul 01, 2016 4:54 pm
by srvet_claudio
Rathinagiri wrote:My two cents:

1. We can use aSort() function
2. Before we start we can use DisableUpdate and after we finish, use EnableUpdate.
3. Add another parameter to allow ascending/descending order.

What would be the position for a virtually managed grid?
Very good idea!!!

With virtual grid, you have to provide the already sorted data when they are loaded with the query method

Re: Grid Sort (Function / Method)

Posted: Fri Jul 01, 2016 9:24 pm
by Roberto Lopez
serge_girard wrote:Hi,
I use ON HEADCLICK to sort a grid on a specific field. Like this:
<...>
So for every field the sort function is called. In the SORT_GRID_TREE some fields need to be sorted as numerics (but displayed as chars).
This is the most unconvenience I found. But overall it works great.
<...>
And... dreaming a little... You could set an arrow icon for the column header clicked, to indicate that it is sorted and switch from ascending to descending when you click it again (changing the icon of course)

Re: Grid Sort (Function / Method)

Posted: Fri Jul 01, 2016 9:34 pm
by Roberto Lopez
Rathinagiri wrote:My two cents:

1. We can use aSort() function
2. Before we start we can use DisableUpdate and after we finish, use EnableUpdate.
3. Add another parameter to allow ascending/descending order.

What would be the position for a virtually managed grid?
I agree with Claudio: Very good ideas.

Regarding virtual grid: ListView requires the data it needs, according the user navigation actions (among other things) so, there is nothing more to do, that assure that the data be sorted when is required to show it.

Re: Grid Sort (Function / Method)

Posted: Fri Jul 01, 2016 10:35 pm
by esgici
Hi

Are you about something like this :?:

Viva HMG :D

Re: Grid Sort (Function / Method)

Posted: Sat Jul 02, 2016 7:57 am
by serge_girard
It seems Esgici did this MANY years ago! Bravo!

Serge