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.
I don’t know if you know, but you can implement a custom sort on arrays, see the bottom half of http://documentation.xojo.com/api/language/sort.html so you could use https://documentation.xojo.com/api/text/str.htmlComp in combination with this to make your own case-sensitive array sorter.
That’s a great tip Julian, I’ll check it out…
Real coders would implement their own QuickSort algorithm…
Smart coders don’t reinvent the wheel
But you’re right it probably comes to that…
It won’t, the delegate technique Julian suggested will work just fine.
[quote=453532:@Kem Tekinay]Real coders would implement their own QuickSort algorithm…
bubble sort all the way!
and I don’t mean about the suggestion… I mean about waiting for a bubble sort to complete
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
Private Function SortBCompare(Value1 as string, value2 as string) As integer
return StrComp(value1, value2, REALbasic.StrCompCaseSensitive)
And for a quick basic test:
dim strArr() as string = Array("a","B")
MsgBox "Sort: " + Join(strArr) + chr(10) + "SortB: " + Join(SortB(strArr))
Thanks for your help with this Jullian!
PS. for non-ascii I would use Realbasic.StrCompLexical instead of REALbasic.StrCompCaseSensitive
Paul Lefebvre has officially blogged about delegated case-sensitive sort here so Julian’s approach to this problem is now Xojo-blessed!
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!
fast SortB? do I really need to grap my old PowerBASIC copy?