string comparisons in .NET

  • Thread starter Thread starter Steve Long
  • Start date Start date
S

Steve Long

Hello,
I am looking for some opinions the way that the:
string.Compare method returns results in the .NET Framework.

I'm building a strings object that inserts strings in sorted order so I
would like to know your expectations about this topic.

Example 1:
In the following, each line is a single string:
1
12
124
111

Using the string.Compare method "111" compares less than "12" so inserting
it into my object, the sort order is:
1
111
12
124


Example 2:
a
ab
abc
aaa

Again, string.Compare compares "aaa" less than "ab" so the sort order goes:
a
aaa
ab
abc

Is this behavior what you would expect and how you would use the
string.Compare method? It seems to me that "aaa" should be greater than "ab"
and that "111" should be greater than "12".

Your thoughts are much appreciated concerning how you would approach this.

Steve Long
 
Steve Long said:
I am looking for some opinions the way that the:
string.Compare method returns results in the .NET Framework.

I'm building a strings object that inserts strings in sorted order so I
would like to know your expectations about this topic.

<snip>

I'd intuitively expect lexicographic ordering, which is what I get. I'd
also look in the documentation to see what to expect.

I can't see why you'd expect "aaa" to be greater than "ab". Do you
expect "aardvark" to come after "as" in the dictionary, for instance?
That's the kind of comparison you get with String.Compare.
 
why so? character '1' comes before character '2', character 'a' comes before character 'b'. it sorts exactly how it would be arranged in a dictionary. length has nothing to do with it

----- Steve Long wrote: ----

Is this behavior what you would expect and how you would use th
string.Compare method? It seems to me that "aaa" should be greater than "ab
and that "111" should be greater than "12"

Your thoughts are much appreciated concerning how you would approach this

Steve Lon
 
Okay, I get it. Thanks

Steve

Jon Skeet said:
<snip>

I'd intuitively expect lexicographic ordering, which is what I get. I'd
also look in the documentation to see what to expect.

I can't see why you'd expect "aaa" to be greater than "ab". Do you
expect "aardvark" to come after "as" in the dictionary, for instance?
That's the kind of comparison you get with String.Compare.
 
Back
Top