Skip to content

Case-Sensitive Array Sorting

A customer on the forum was asking for a way to do case-sensitive array sorting. The built-in array Sort command uses case-insensitive sorting and they wanted an alternative.

It turns out it is super-easy to use the Sort(Delegate) method to create your own sorting method and then pass it to the call to sort.

The sorting method would use StrComp to get a case-sensitive sort and could look like this:

Public Function CaseSensitiveSort(value1 As String, value2 As String) as Integer
   Return StrComp(value1, value2, Realbasic.StrCompCaseSensitive)
End Function

And then you can call it like this:

Dim s() As String = Array("alpha", "Test", "Beta", "candy")
s.Sort(AddressOf CaseSensitiveSort)

To get this sorted result:

  • Beta
  • Test
  • alpha
  • candy