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

  1. 7 months ago

    Eric W

    10 Sep 2019 Pre-Release Testers, Xojo Pro
    Edited 7 months ago

    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?

    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!

  2. Julian S

    10 Sep 2019 Pre-Release Testers, Xojo Pro UK
    Edited 7 months ago

    I don't know if you know, but you can implement a custom sort on arrays, see the bottom half of http://docs.xojo.com/Sort so you could use https://docs.xojo.com/StrComp in combination with this to make your own case-sensitive array sorter.

  3. Eric W

    10 Sep 2019 Pre-Release Testers, Xojo Pro

    That's a great tip Julian, I'll check it out...

  4. Kem T

    10 Sep 2019 Pre-Release Testers, Xojo Pro, XDC Speakers, MVP Connecticut

    Real coders would implement their own QuickSort algorithm...

    :P

  5. Eric W

    10 Sep 2019 Pre-Release Testers, Xojo Pro

    Smart coders don't reinvent the wheel ;-)

  6. Eric W

    10 Sep 2019 Pre-Release Testers, Xojo Pro

    But you're right it probably comes to that...

  7. Kem T

    10 Sep 2019 Pre-Release Testers, Xojo Pro, XDC Speakers, MVP Connecticut

    :)

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

  8. brian f

    10 Sep 2019 Pre-Release Testers, Xojo Pro Chilly California

    @Kem T Real coders would implement their own QuickSort algorithm...

    :P

    Pffft.....
    bubble sort all the way! ;)

  9. Dave S

    10 Sep 2019 San Diego, California USA

    @brian f bubble sort all the way!

    yawn....

    and I don't mean about the suggestion... I mean about waiting for a bubble sort to complete :)

  10. Dave S

    10 Sep 2019 San Diego, California USA

    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

  11. Eric W

    10 Sep 2019 Pre-Release Testers, Xojo Pro
    Edited 7 months ago

    OK this seems to work, a global SortB function supported by a private SortBCompare deligate:

    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 Function

    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:

    Sort: aB
    SortB: Ba

    Thanks for your help with this Jullian!

    PS. for non-ascii I would use Realbasic.StrCompLexical instead of REALbasic.StrCompCaseSensitive

  12. Eric W

    23 Sep 2019 Pre-Release Testers, Xojo Pro Answer
    Edited 7 months ago

    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!

  13. Tomas J

    23 Sep 2019 Pre-Release Testers, Xojo Pro Europe (Germany)
    Edited 7 months ago

    fast SortB? do I really need to grap my old PowerBASIC copy? ;-)

  14. Eric W

    23 Sep 2019 Pre-Release Testers, Xojo Pro

    Not anymore.

  15. Norman P

    23 Sep 2019 Pre-Release Testers, Xojo Pro outside enjoying the fresh air

    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/

or Sign Up to reply!