Sorting []

Why does the following code

[code]dim test(-1) as string
test.Append “[test]”
test.Append “bla”
test.Append “blubber”

test.Sort
Beep[/code]

result in an array with “[test]” at the end and not the beginning? I recently noticed that Valentina sorts this at the beginning and Xojo at the end. The Valentina database is set to German locale, Strength = kSecondary (case-insensitive as I remember).

In Xojo I would simply use a different sort comparator but the sorting is a tiny bit more complicated:

[code]dim SortMailboxes(-1) as String
for currentMailbox as Integer = 0 to UBound(AllMailFoldersUI)

dim firstPart as String = NthField(AllMailFoldersUI(currentMailbox), globals.MailboxDivider, 1)
dim SortMailbox as String = ReplaceAll(AllMailFoldersUI(currentMailbox), globals.MailboxDivider, Chr(1))

if firstPart = “Inbox” then
SortMailboxes.Append “1:” + Lowercase(SortMailbox)
elseif firstPart = “Sent Messages” then
SortMailboxes.Append “2:” + Lowercase(SortMailbox)
elseif firstPart = “Trash” then
SortMailboxes.Append “6:” + Lowercase(SortMailbox)
elseif firstPart = “Spam” then
SortMailboxes.Append “3:” + Lowercase(SortMailbox)
elseif firstPart = “Mailboxes” then
SortMailboxes.Append “4:” + Lowercase(SortMailbox)
else
SortMailboxes.Append “5:” +Lowercase(SortMailbox)
end if

next

SortMailboxes.SortWith(AllMailfoldersUI, AllMailFolders)
[/code]

Bug or feature?

Xojo 2017r3, macOS 10.11.6, 32 and 64bit.

The array sort methods are apparently case-insensitive and “[” comes after “B”.

You could try using the Sort(sortMethod As Delegate) and provide your own method to do case-sensitive sorting.

Xojo string comparisons are case-insensitive. See https://forum.xojo.com/32834-case-sensitive-string-comparison/0

Further testing indicates that Text sorting is case-sensitive. So it you use an array of Text then “[blah]” is at the beginning.

Using Text is not an option because this is part of a relatively complex algorithm.

The order of the ASCII characters is (simplified) : A-Z, [], a-z . Where should the [] fall if I do case-insensitive sorting?

How do I use the sort delegate method with SortWith? My sort method sorts first the Inbox, then the Sent Messages and so on. I need the SortWith because I have 2 arrays.

Just rely on StrComp in the delegate method for that?

Don’t use separate arrays. Make a class with the values as properties and sort that class array using the delegate method.

@Paul Lefebvre : if you use StrComp then the [] would sort at the top and not the bottom.

Scratching the head about what this means: [quote]Don’t use separate arrays. Make a class with the values as properties and sort that class array using the delegate method.[/quote] There are three arrays, all of which are different and have a different sort order.