Page 1 of 1

CustomSortList,CustomSortArray for own comparison procedures

Posted: Wed Aug 11, 2010 11:38 pm
by STARGÅTE
Hello, it would be good if there would be a user-sorting.

Currently I use a macro like this: (its slow)

Code: Select all

Prototype.i CustomSortListComparisonCallback(*Element1, *Element2)
Macro CustomSortList(ListName, ComparisonCallback)
  CustomSortList_ComparisonCallback.CustomSortListComparisonCallback = ComparisonCallback
  Repeat
    CustomSortList_Quit = #True
    ForEach ListName
      *CustomSortList_Element = @ListName
      While NextElement(ListName)
        If CustomSortList_ComparisonCallback(*CustomSortList_Element, ListName)
          SwapElements(ListName, *CustomSortList_Element, @ListName)
          CustomSortList_Quit = #False
        EndIf
      Wend
      ChangeCurrentElement(ListName, *CustomSortList_Element)
    Next
  Until CustomSortList_Quit
EndMacro
For example, I can sort multiple structure fields simultaneously.

Code: Select all

Structure Test 
  a.i
  b.i
EndStructure

Procedure Comparison(*Element1.Test, *Element2.Test)
  If *Element1\a > *Element2\a
    ProcedureReturn #True
  ElseIf *Element1\a = *Element2\a
    If *Element1\b > *Element2\b
      ProcedureReturn #True
    Else
      ProcedureReturn #False
    EndIf
  Else
    ProcedureReturn #False
  EndIf
EndProcedure

NewList Test.Test()

RandomSeed(0)
For n = 1 To 10
  AddElement(Test())
  Test()\a = Random(5)
  Test()\b = Random(5)
  Debug Str(Test()\a)+" , "+Str(Test()\b)
Next

CustomSortList(Test(), @Comparison())
Debug "----"

ForEach Test()
  Debug Str(Test()\a)+" , "+Str(Test()\b)
Next
The Syntax is:
CustomSortList(ListName(), *ComparisonCallback)

ComparisonCallback is a procedure that either returns 1 (swap) or 0 (no swap).

Re: CustomSortList,CustomSortArray for own comparison proced

Posted: Thu Aug 12, 2010 9:29 am
by Trond
For example, I can sort multiple structure fields simultaneously.
To do that with a list you can just sort with the normal PB functions. First sort the list on the least important field, then on the more important field, then last on the most important field.

Re: CustomSortList,CustomSortArray for own comparison proced

Posted: Thu Aug 12, 2010 9:56 am
by gnozal
Yes, according to freak, "since PB 4.30, SortStructuredList (and SortList) use Mergesort which is a stable sort, so if you first sort all the list by titles and then again by album you will get a list which is sorted by album and each album is sorted by title.
Note that this does not work with Arrays, as SortArray uses Quicksort which is unstable. (ie the sorting of the secondary key would be lost)".

Re: CustomSortList,CustomSortArray for own comparison proced

Posted: Thu Aug 12, 2010 10:53 am
by Fred
Without relying on internal implementation, you can sort on several successive fields by sorting according your first item, then using the range of the sort function to sort your subfields.

Re: CustomSortList,CustomSortArray for own comparison proced

Posted: Thu Aug 12, 2010 2:12 pm
by STARGÅTE
That was just one example of many.
For more fields, there may be another solution with normal sorting functions.

But there are just many more sorting options, which would be nice quick with CustomSortList.
For now I will just continue to use my Macro :wink: