String sorting issue in C# -
String sorting issue in C# -
i have list this
list<string> items = new list<string>(); items.add("-"); items.add("."); items.add("a-"); items.add("a."); items.add("a-a"); items.add("a.a"); items.sort(); string output = string.empty; foreach (string s in items) { output += s + environment.newline; } messagebox.show(output);
the output coming as
- . a- a. a.a a-a
where expecting results as
- . a- a. a-a a.a
any thought why "a-a" not coming before "a.a" "a-" comes before "a."
if want string sort based on actual byte value opposed rules defined current civilization can sort ordinal:
items.sort(stringcomparer.ordinal);
this create results consistent across cultures (but produce unintuitive sortings of "14" coming before "9" may or may not you're looking for).
c# string sorting cultureinfo
Comments
Post a Comment