Skip to content

JSArray.Sort

boxgaming edited this page Jul 25, 2026 · 3 revisions

Sorts the elements of an array.

Syntax

JSArray.Sort a [, compareFn]

Parameters

  • The a parameter contains the native Javascript array on which the operation will be performed.

  • The optional compareFn parameter is a reference to a function that determines the order of the elements. The function is called with the following arguments:

    • a
      The first element for comparison. Will never be undefined.
    • b
      The second element for comparison. Will never be undefined.

    It should return a number where:

    • A negative value indicates that a should come before b.
    • A positive value indicates that a should come after b.
    • Zero or NaN indicates that a and b are considered equal.

    If omitted, the array elements are converted to strings, then sorted according to each character's Unicode code point value.

Examples

Example 1: Default sort function

Import JSArray From "lib/lang/array.bas"

Dim colors As Object
colors = JSArray.Create("red", "green", "blue", "orange", "yellow")

JSArray.Sort colors
Print colors

Output

blue,green,orange,red,yellow

Example 2: User-defined sort functions

Import JSArray From "lib/lang/array.bas"

Dim numbers As Object
numbers = JSArray.Create(5, 27, 18, 9, 4, 22, 20)

JSArray.Sort numbers, @SortAsc
Print JSArray.Join(numbers)

JSArray.Sort numbers, @SortDesc
Print JSArray.Join(numbers)


Function SortAsc (a, b)
    SortAsc = a - b
End Function

Function SortDesc (a, b)
    SortDesc = b - a
End Function

Output

4,5,9,18,20,22,27
27,22,20,18,9,5,4

Example 3: Sorting arrays of custom data types

Import JSArray From "lib/lang/array.bas"

Type Person
    As String firstName, lastName
    As Integer age
End Type

Dim people As Object
people = JSArray.Create( _
            CreatePerson("John", "Doe", 23), _
            CreatePerson("Andrew", "Jones", 45), _
            CreatePerson("Mary", "Barnes", 19), _
            CreatePerson("Betty", "Doe", 38), _
            CreatePerson("Howard", "Anderson", 28))

Print JSArray.Reduce(people, @ToString, "")
Print
JSArray.Sort people, @SortByAge
Print JSArray.Reduce(people, @ToString, "")
Print
JSArray.Sort people, @SortByName
Print JSArray.Reduce(people, @ToString, "")

Function ToString (accumulator, p As Person, index)
    If index > 0 Then accumulator = accumulator + Chr$(10)
    ToString = accumulator + p.lastName + ", " + p.firstName + " - " + p.age
End Sub

Function SortByName (a, b)
    Dim As String aname, bname
    aname = a.lastName
    bname = b.lastName
    If aname = bname Then
        aname = a.firstName
        bname = b.firstName
    End If
    
    If aname > bname Then
        SortByName = 1
    ElseIf aname < bname Then
        SortByName = -1
    Else
        SortByName = 0
    EndIf
End Function

Function SortByAge (a, b)
    SortByAge = a.age - b.age
End Function

Function CreatePerson (firstName As String, lastName As String, age As Integer)
    Dim p As Person
    p.firstName = firstName
    p.lastName = lastName
    p.age = age
    CreatePerson = p
End Function

Output

Doe, John - 23
Jones, Andrew - 45
Barnes, Mary - 19
Doe, Betty - 38
Anderson, Howard - 28

Barnes, Mary - 19
Doe, John - 23
Anderson, Howard - 28
Doe, Betty - 38
Jones, Andrew - 45

Anderson, Howard - 28
Barnes, Mary - 19
Doe, Betty - 38
Doe, John - 23
Jones, Andrew - 45

See Also

JSArray.Create
JSArray.Reduce
Javascript - Array.sort()

Clone this wiki locally