c# - Inserting spaces between chars of two strings modify their order -
this question has answer here:
i have 2 strings of same length.
assuming (probably wrongly) inserting space between each character of each string not change order.
var e1 = "12*4"; var e2 = "12-4"; console.writeline(string.compare(e1,e2)); // -1 (e1 < e2) var f1 = "1 2 * 4"; var f2 = "1 2 - 4"; console.writeline(string.compare(f1,f2)); // +1 (f1 > f2)
if insert other characters (_ x instance), order preserved.
what's going on ?
thanks in advance.
if use ordinal comparison, right result.
the reason ordinal comparison works evaluating numeric value of each of chars in string object, inserting spaces make no difference.
if use other types of comparisons, there other things involved. documentation:
an operation uses word sort rules performs culture-sensitive comparison wherein nonalphanumeric unicode characters might have special weights assigned them. using word sort rules , conventions of specific culture, hyphen ("-") might have small weight assigned "coop" , "co-op" appear next each other in sorted list.
an operation uses ordinal sort rules performs comparison based on numeric value (unicode code point) of each char in string. ordinal comparison fast culture-insensitive. when use ordinal sort rules sort strings start unicode characters (u+), string u+xxxx comes before string u+yyyy if value of xxxx numerically less yyyy.
Comments
Post a Comment