I have code that sorts an ArrayList of elements based on one attribute called ‘title’ which is of type String. The code uses Collator like this:
Collator( Collator collator = Collator.getInstance(); ).
I have two objects with title “@a” and the other object has title “#a”
I pass these objects as a List and call
Collections.sort(list,comparator)
This gives the order as
"@a" "#a"
Why is “#a” appearing last even though its ASCII value is less than “@a” ?
My clean-room implementation:
Output:
This code doesn’t reproduce your problem.
For reference:
‘#’ is 0x23
‘@’ is 0x40
Everything looks normal.
EDIT: new code following your comment “The code uses Collator but its used as
Collator collator = Collator.getInstance();not specific to any locale.”:Output:
This reproduces your problem.
If I use
Collator.getInstance()to sort the ASCII table, this is the output I get:You can see this is quite different from the ASCII collating order:
For OP’s interest, this is the code used to create this output: