Grid Sort (Function / Method)

Source code related resources

Moderator: Rathinagiri

User avatar
Roberto Lopez
HMG Founder
Posts: 4004
Joined: Wed Jul 30, 2008 6:43 pm

Grid Sort (Function / Method)

Post 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.
Regards/Saludos,

Roberto


(Veritas Filia Temporis)
User avatar
srvet_claudio
Posts: 2193
Joined: Thu Feb 25, 2010 8:43 pm
Location: Uruguay
Contact:

Re: Grid Sort (Function / Method)

Post by srvet_claudio »

Very nice, you used "burbuja" as sort method?
I will adapted for Unicode.
Best regards.
Dr. Claudio Soto
(from Uruguay)
http://srvet.blogspot.com
User avatar
Roberto Lopez
HMG Founder
Posts: 4004
Joined: Wed Jul 30, 2008 6:43 pm

Re: Grid Sort (Function / Method)

Post 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!
Regards/Saludos,

Roberto


(Veritas Filia Temporis)
User avatar
Rathinagiri
Posts: 5471
Joined: Tue Jul 29, 2008 6:30 pm
DBs Used: MariaDB, SQLite, SQLCipher and MySQL
Location: Sivakasi, India
Contact:

Re: Grid Sort (Function / Method)

Post 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?
East or West HMG is the Best.
South or North HMG is worth.
...the possibilities are endless.
User avatar
serge_girard
Posts: 3165
Joined: Sun Nov 25, 2012 2:44 pm
DBs Used: 1 MySQL - MariaDB
2 DBF
Location: Belgium
Contact:

Re: Grid Sort (Function / Method)

Post 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
There's nothing you can do that can't be done...
User avatar
srvet_claudio
Posts: 2193
Joined: Thu Feb 25, 2010 8:43 pm
Location: Uruguay
Contact:

Re: Grid Sort (Function / Method)

Post 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
Best regards.
Dr. Claudio Soto
(from Uruguay)
http://srvet.blogspot.com
User avatar
Roberto Lopez
HMG Founder
Posts: 4004
Joined: Wed Jul 30, 2008 6:43 pm

Re: Grid Sort (Function / Method)

Post 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)
Regards/Saludos,

Roberto


(Veritas Filia Temporis)
User avatar
Roberto Lopez
HMG Founder
Posts: 4004
Joined: Wed Jul 30, 2008 6:43 pm

Re: Grid Sort (Function / Method)

Post 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.
Regards/Saludos,

Roberto


(Veritas Filia Temporis)
User avatar
esgici
Posts: 4543
Joined: Wed Jul 30, 2008 9:17 pm
DBs Used: DBF
Location: iskenderun / Turkiye
Contact:

Re: Grid Sort (Function / Method)

Post by esgici »

Hi

Are you about something like this :?:

Viva HMG :D
Viva INTERNATIONAL HMG :D
User avatar
serge_girard
Posts: 3165
Joined: Sun Nov 25, 2012 2:44 pm
DBs Used: 1 MySQL - MariaDB
2 DBF
Location: Belgium
Contact:

Re: Grid Sort (Function / Method)

Post by serge_girard »

It seems Esgici did this MANY years ago! Bravo!

Serge
There's nothing you can do that can't be done...
Post Reply