Skip to content

Modern Sorting SortBy

Paul Squires edited this page Oct 19, 2026 · 5 revisions

Home › Sorting › Sorting › SortBy

SortBy

Sorts an Array in place by your comparer; equal elements may change places.

Syntax

SortBy( a, less )

Notes

  • O(n log n) worst — in place, unstable

Examples

#import "std/containers"
using containers

dim v as Array( of ustring )
v.Push( "pear" ) : v.Push( "fig" ) : v.Push( "banana" )

'' A caller-supplied strict '<' -- by length here, not alphabetically
SortBy( v, function( byref x as ustring, byref y as ustring ) as boolean
    return len( x ) < len( y )
end function )

for i as int32 = 0 to v.Count - 1
    print v[ i ]
next                         '' fig / pear / banana

'' Taken as a generic rather than a procedure pointer, so a CAPTURING
'' lambda works too -- a closure object is not a procptr

Library: Sorting
Category: Sorting
Documented under: Procedures

Clone this wiki locally