In many cultures (including the invariant culture) the hyphen is a character that is of only minor importance for sorting purposes. In most texts, this makes sense: pre-whatever
and prewhatever
are pretty similar. For example, the following list is sorted as this, which I think is good:
preasdf
prewhatever
pre-whatever
prezxcv
You seem to want an Ordinal
comparison, where values are compared purely by their unicode code point values. If you change the line to:
lines.Sort(StringComparer.Ordinal);
Then your results are:
x-00000000000000000020.000000000
x000000000000000000037.000000000
x000000000000000000093.000000000
x000000000000000000538.000000000
x000000000000000100000.000000000
If you're wondering why the -...20.0
value ended up where it did, consider what it'd look like if you removed the -
(and compare with the above pre
list).
x000000000000000000037.000000000
x000000000000000000093.000000000
x00000000000000000020.000000000
x000000000000000000538.000000000
x000000000000000100000.000000000
If your input is always in the format x[some number]
, I'd parse the value after x
as a decimal
or double
, and do the sorting on that. That would make it easier to ensure expected behavior, and overall better.