Page 1 of 1

Sort by user callback function

Posted: Fri May 05, 2006 9:28 am
by breeze4me
SortArrayByCallback(...... , @CallbackFunction, ...)
SortListByCallback(...... , @CallbackFunction, ...)
SortStructuredArrayByCallback(...... , @CallbackFunction, ...)
SortStructuredListByCallback(...... , @CallbackFunction, ...)
user custom callback function is like below.

Code: Select all

Procedure.l CallbackFunction(1st_Element, 2nd_Element)
  ...
  ProcedureReturn -1(or 1 or 0)
EndProcedure
It is sometimes necessary.

Native PB's sort commands show somewhat different result from expectative orders on non-English Windows, because of foreign language characters.

For example, displayed file order on ExplorerListGadget(with #PB_Explorer_AutoSort flag) is different from Windows Explorer's file order on Korean Windows. So i made custom ExplorerList using ListIconGadget(with #LVM_SORTITEMS for using custom sort function).
Not using this method, if there are native sort finctions by callback, more powerful manipulating will be possible.

Code: Select all

Dim a.s(10)
a(0)=" _"
a(1)="aa"
a(2)="_A"
a(3)="가"  ;Korean character
a(4)="1a"
a(5)="AD"
a(6)="05"
a(7)="Z8"
a(8)="하"  ;Korean character
a(9)="__"
a(10)="*/"
For i=0 To 10
  For j=0 To 10
    If lstrcmpi_(a(i),a(j)) < 0
      Swap a(i),a(j)
    EndIf
  Next
Next
Debug "Win API(the same order as Windows Explorer's file listing order)"
For i=0 To 10
  Debug a(i)
Next
Result
Win API(the same orders as Windows Explorer's file listing order)
_
*/
__
_A
05
1a
가 ;Korean character
하 ;Korean character
aa
AD
Z8

Code: Select all

Dim a.s(10)
a(0)=" _"
a(1)="aa"
a(2)="_A"
a(3)="가"  ;Korean character
a(4)="1a"
a(5)="AD"
a(6)="05"
a(7)="Z8"
a(8)="하"  ;Korean character
a(9)="__"
a(10)="*/"
SortArray(a(),2)
Debug "PB Native(maybe ExplorerListGadget's order)"
For i=0 To 10
  Debug a(i)
Next
Result
PB Native(maybe ExplorerListGadget's order)
_
*/
05
1a
__
_A
aa
AD
Z8
가 ;Korean character
하 ;Korean character
Besides, below coding will be possible.

Code: Select all

Procedure.l SortFunction(*a, *b)
  
  z = lstrcmpi_(*a, *b)
  
  If z=0  ;a=b
    Do something...
  EndIf
  
  ProcedureReturn z
EndProcedure

Dim a.s(10)
SortArray(a(), @SortFunction)

Posted: Fri May 05, 2006 10:00 pm
by josku_x
This would be a good way to implement your own sorting too; easier than what you know have to do (loop through the array and create a temp array where to store stuff etc...)..