CustomSortList,CustomSortArray for own comparison procedures

Got an idea for enhancing PureBasic? New command(s) you'd like to see?
User avatar
STARGÅTE
Addict
Addict
Posts: 2227
Joined: Thu Jan 10, 2008 1:30 pm
Location: Germany, Glienicke
Contact:

CustomSortList,CustomSortArray for own comparison procedures

Post 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).
PB 6.01 ― Win 10, 21H2 ― Ryzen 9 3900X, 32 GB ― NVIDIA GeForce RTX 3080 ― Vivaldi 6.0 ― www.unionbytes.de
Lizard - Script language for symbolic calculations and moreTypeface - Sprite-based font include/module
Trond
Always Here
Always Here
Posts: 7446
Joined: Mon Sep 22, 2003 6:45 pm
Location: Norway

Re: CustomSortList,CustomSortArray for own comparison proced

Post 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.
gnozal
PureBasic Expert
PureBasic Expert
Posts: 4229
Joined: Sat Apr 26, 2003 8:27 am
Location: Strasbourg / France
Contact:

Re: CustomSortList,CustomSortArray for own comparison proced

Post 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)".
For free libraries and tools, visit my web site (also home of jaPBe V3 and PureFORM).
Fred
Administrator
Administrator
Posts: 18162
Joined: Fri May 17, 2002 4:39 pm
Location: France
Contact:

Re: CustomSortList,CustomSortArray for own comparison proced

Post 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.
User avatar
STARGÅTE
Addict
Addict
Posts: 2227
Joined: Thu Jan 10, 2008 1:30 pm
Location: Germany, Glienicke
Contact:

Re: CustomSortList,CustomSortArray for own comparison proced

Post 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:
PB 6.01 ― Win 10, 21H2 ― Ryzen 9 3900X, 32 GB ― NVIDIA GeForce RTX 3080 ― Vivaldi 6.0 ― www.unionbytes.de
Lizard - Script language for symbolic calculations and moreTypeface - Sprite-based font include/module
Post Reply