I’m looking for a case-sensitive sort function for a string array because the built-in .sort is case insensitive. Presently the quickest thing I can think of is to shell to Linux Sort command which involves reformatting data into lines and back again plus context-switching overheads. A SortB would be nice but sadly no.
Stick the data into a in-memory SQLite table… there the sort (ORDER BY) is not case-sensitive unless you apply NOCASE to the table attributes ahead of time
OK this seems to work, a global SortB function supported by a private SortBCompare deligate:
[code]Function SortB(StringArray() as string) As string()
if StringArray.Ubound > -1 then
StringArray.Sort(AddressOf SortBCompare)
return StringArray
end if
End Function
Private Function SortBCompare(Value1 as string, value2 as string) As integer
return StrComp(value1, value2, REALbasic.StrCompCaseSensitive)
End Functio[/code]n
And for a quick basic test:
Sub Action()
dim strArr() as string = Array("a","B")
strArr.Sort
MsgBox "Sort: " + Join(strArr) + chr(10) + "SortB: " + Join(SortB(strArr))
End Sub
Returns:
[quote]Sort: aB
SortB: Ba[/quote]
Thanks for your help with this Jullian!
PS. for non-ascii I would use Realbasic.StrCompLexical instead of REALbasic.StrCompCaseSensitive
My implementation’s test for empty arrays was not needed because I misinterpreted the warning about NIL in the documentation as applying to empty string arrays when it was only referring to the existence of the delegate code itself. My code although workable was also VBish (old habits die hard) with strings – which in VB are/were reference types that changed to byVals if modified – but in Xojo are not with arrays – see blog by Javier Menendez here.
Once again, thanks Julian for the concept. It saved me heaps of time!