Sort

Sorting and shuffling arrays and lists. Arrays and lists are passed with empty brackets: SortArray(scores(), #PB_Sort_Ascending).

The sort is stable: elements that compare equal keep their order. That makes it easy to sort by one key and then another.

Option Meaning
#PB_Sort_Ascending lowest first (the default)
#PB_Sort_Descending highest first
#PB_Sort_NoCase strings: a and A sort together (A–Z and Latin-1 letters). Combine with \|

Start and End limit the sort to the elements from index Start to index End. List positions count from 0. Without them the whole array or list is sorted.

SortArray ✅

SortArray(Array(), Options [, Start, End])

Sorts an array of numbers or strings. For an array of structures, use SortStructuredArray.

OpenConsole()
Dim n.i(5)
n(0) = 42 : n(1) = -7 : n(2) = 15 : n(3) = 0 : n(4) = 99 : n(5) = 3
SortArray(n(), #PB_Sort_Ascending)
s$ = ""
For i = 0 To 5 : s$ + Str(n(i)) + " " : Next
PrintN(RTrim(s$))
SortArray(n(), #PB_Sort_Descending, 1, 4)
s$ = ""
For i = 0 To 5 : s$ + Str(n(i)) + " " : Next
PrintN(RTrim(s$))

Output:

-7 0 3 15 42 99
-7 42 15 3 0 99

Strings sort by character code, so capitals come before lower case unless #PB_Sort_NoCase is given:

OpenConsole()
Dim w.s(4)
w(0) = "pear" : w(1) = "Apple" : w(2) = "banana" : w(3) = "apple" : w(4) = "Cherry"
SortArray(w(), #PB_Sort_Ascending)
s$ = ""
For i = 0 To 4 : s$ + w(i) + " " : Next
PrintN(RTrim(s$))
SortArray(w(), #PB_Sort_Ascending | #PB_Sort_NoCase)
s$ = ""
For i = 0 To 4 : s$ + w(i) + " " : Next
PrintN(RTrim(s$))

Output:

Apple Cherry apple banana pear
Apple apple banana Cherry pear

Empty strings sort first in ascending order, and last in descending order.

SortList ✅

SortList(List(), Options [, Start, End])

Sorts a list of numbers or strings. The elements are relinked, not copied, so pointers to elements stay valid.

OpenConsole()
NewList t.d()
AddElement(t()) : t() = 2.5
AddElement(t()) : t() = -1.25
AddElement(t()) : t() = 10
AddElement(t()) : t() = 0.5
SortList(t(), #PB_Sort_Descending)
s$ = ""
ForEach t() : s$ + StrD(t(), 2) + " " : Next
PrintN(RTrim(s$))

Output:

10.00 2.50 0.50 -1.25

SortStructuredArray ✅

SortStructuredArray(Array(), Options, OffsetOf(Structure\Field), TypeOf(Structure\Field) [, Start, End])

Sorts an array of structures by one field. OffsetOf gives the field's position and TypeOf its type. The type can also be written as a constant: #PB_Byte, #PB_Ascii, #PB_Word, #PB_Unicode, #PB_Character, #PB_Long, #PB_Integer, #PB_Quad, #PB_Float, #PB_Double or #PB_String.

Structure Player
  name$
  score.l
EndStructure

OpenConsole()
Dim p.Player(3)
p(0)\name$ = "Cy"  : p(0)\score = 30
p(1)\name$ = "Bob" : p(1)\score = 50
p(2)\name$ = "Ann" : p(2)\score = 30
p(3)\name$ = "Di"  : p(3)\score = 10
SortStructuredArray(p(), #PB_Sort_Descending, OffsetOf(Player\score), TypeOf(Player\score))
For i = 0 To 3
  PrintN(p(i)\name$ + " " + Str(p(i)\score))
Next
SortStructuredArray(p(), #PB_Sort_Ascending, OffsetOf(Player\name$), #PB_String)
PrintN(p(0)\name$ + " " + p(1)\name$ + " " + p(2)\name$ + " " + p(3)\name$)

Output:

Bob 50
Cy 30
Ann 30
Di 10
Ann Bob Cy Di

Cy stays ahead of Ann: they have the same score, and the sort is stable.

SortStructuredList ✅

SortStructuredList(List(), Options, OffsetOf(Structure\Field), TypeOf(Structure\Field) [, Start, End])

The same for a list of structures.

Structure Item
  price.d
  name$
EndStructure

OpenConsole()
NewList shop.Item()
AddElement(shop()) : shop()\name$ = "tea"    : shop()\price = 3.5
AddElement(shop()) : shop()\name$ = "bread"  : shop()\price = 1.2
AddElement(shop()) : shop()\name$ = "cheese" : shop()\price = 4.75
SortStructuredList(shop(), #PB_Sort_Ascending, OffsetOf(Item\price), TypeOf(Item\price))
ForEach shop()
  PrintN(shop()\name$ + " " + StrD(shop()\price, 2))
Next

Output:

bread 1.20
tea 3.50
cheese 4.75

RandomizeArray ✅

RandomizeArray(Array() [, Start, End])

Shuffles the elements into a random order. It uses Random, so RandomSeed makes the order repeatable.

OpenConsole()
Dim d.i(9)
For i = 0 To 9 : d(i) = i : Next
RandomizeArray(d())
sum = 0
For i = 0 To 9 : sum + d(i) : Next
SortArray(d(), #PB_Sort_Ascending)
PrintN(Str(sum) + " " + Str(d(0)) + " " + Str(d(9)))

Output:

45 0 9

RandomizeList ✅

RandomizeList(List() [, Start, End])

Shuffles a list by relinking its elements.

OpenConsole()
NewList c.s()
For i = 1 To 5
  AddElement(c()) : c() = Chr('a' + i - 1)
Next
RandomizeList(c())
SortList(c(), #PB_Sort_Ascending)
s$ = ""
ForEach c() : s$ + c() : Next
PrintN(s$ + " " + Str(ListSize(c())))

Output:

abcde 5

CustomSortArray ✅

CustomSortArray(Array(), @Compare() [, Options [, Start, End]])

Sorts with your own comparison: Compare(*a, *b) gets pointers to two elements and returns #PB_Sort_Lesser if the first goes before the second, #PB_Sort_Greater if it goes after, or #PB_Sort_Equal. Any negative or positive number works too. #PB_Sort_Descending reverses the order. The sort stays stable.

Structure Person
  name$
  age.l
EndStructure

Procedure.i ByAgeThenName(*a.Person, *b.Person)
  If *a\age <> *b\age
    ProcedureReturn *a\age - *b\age
  EndIf
  If *a\name$ < *b\name$
    ProcedureReturn #PB_Sort_Lesser
  ElseIf *a\name$ > *b\name$
    ProcedureReturn #PB_Sort_Greater
  EndIf
  ProcedureReturn #PB_Sort_Equal
EndProcedure

OpenConsole()
Dim p.Person(3)
p(0)\name$ = "Zoe" : p(0)\age = 30
p(1)\name$ = "Al"  : p(1)\age = 25
p(2)\name$ = "Bo"  : p(2)\age = 30
p(3)\name$ = "Cy"  : p(3)\age = 25
CustomSortArray(p(), @ByAgeThenName())
For i = 0 To 3
  PrintN(p(i)\name$ + " " + Str(p(i)\age))
Next

Output:

Al 25
Cy 25
Bo 30
Zoe 30

CustomSortList ✅

CustomSortList(List(), @Compare() [, Options [, Start, End]])

The same for a list. The pointers point at the element's data. For a list of strings, read it through the built-in String structure:

Procedure.i ByLength(*a.String, *b.String)
  ProcedureReturn Len(*a\s) - Len(*b\s)
EndProcedure

OpenConsole()
NewList words.s()
AddElement(words()) : words() = "banana"
AddElement(words()) : words() = "fig"
AddElement(words()) : words() = "apple"
AddElement(words()) : words() = "kiwi"
CustomSortList(words(), @ByLength())
ForEach words()
  PrintN(words())
Next

Output:

fig
kiwi
apple
banana