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

  1. last week

    Eric W

    Sep 10 Pre-Release Testers, Xojo Pro
    Edited last week

    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?

    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

  2. Julian S

    Sep 10 Pre-Release Testers, Xojo Pro UK
    Edited last week

    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

    Sep 10 Pre-Release Testers, Xojo Pro

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

  4. Kem T

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

    Real coders would implement their own QuickSort algorithm...

    :P

  5. Eric W

    Sep 10 Pre-Release Testers, Xojo Pro

    Smart coders don't reinvent the wheel ;-)

  6. Eric W

    Sep 10 Pre-Release Testers, Xojo Pro

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

  7. Kem T

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

    :)

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

  8. brian f

    Sep 10 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

    Sep 10 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

    Sep 10 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

    Sep 10 Pre-Release Testers, Xojo Pro Answer
    Edited last week

    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

or Sign Up to reply!