Does anyone know of a binary / case-sensitive sort (SortB) function?

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.

Any ideas?

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…

:stuck_out_tongue:

Smart coders don’t reinvent the wheel :wink:

But you’re right it probably comes to that…

:slight_smile:

It won’t, the delegate technique Julian suggested will work just fine.

[quote=453532:@Kem Tekinay]Real coders would implement their own QuickSort algorithm…

:P[/quote]

Pffft…
bubble sort all the way! :wink:

yawn…

and I don’t mean about the suggestion… I mean about waiting for a bubble sort to complete :slight_smile:

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

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? :wink:

Not anymore.

There are a bunch of old blog posts that are related to this topic
https://blog.xojo.com/2019/01/23/byref-vs-reference-types/
https://blog.xojo.com/2019/01/24/some-follow-up-regarding-byref/
https://blog.xojo.com/2018/12/12/what-kind-of-variable-are-you/
https://blog.xojo.com/2016/03/09/easily-sorting-arrays-of-classes/