11

In Java:

"A".compareTo("a"); return -32 //"A" is less than "a".

In .Net, use String.CompareTo:

"A".CompareTo("a"); return 1 //"A" is greater than "a".

In .Net, use Char.CompareTo:

'A'.CompareTo('a'); return -32 //"A" is less than "a".

I know the Java compares string characters using its position in unicode table, but .Net is not. How determines which capital letter is greater than small letter in .Net?

String.CompareTo Method (String)

smartleos
  • 135
  • 1
  • 7
  • i don't get your question in your second line you are comparing A with a and it gives u 1 it means that A is greater than a what do u mean by your question ? – Sora Jun 28 '13 at 06:30
  • Maybe it is because of **locale**. (I cannot speak C# though...) – johnchen902 Jun 28 '13 at 06:32
  • Internally i uses InternalCompareString Windows API function call, but wasn't able to find a documentation on it. – Tigran Jun 28 '13 at 06:41
  • My question is why the .Net determines which "A" is greater than "a", not the return value 1. In .Net, String.CompareTo return 1 (Greater than zero) means This instance is greater than strB. If i use Char.CompareTo: 'A'.CompareTo('a'); return value is -32, that's good, but String.CompareTo return 1, so I want to know why and how. – smartleos Jun 28 '13 at 06:44
  • As I say below, it probably depends on your "culture". No idea what .NET means by that however... – fge Jun 28 '13 at 06:52

3 Answers3

3

The doc I could find says that:

This method performs a word (case-sensitive and culture-sensitive) comparison using the current culture.

So, it is not quite the same as Java's .compareTo() which does a lexicographical comparison by default, using Unicode code points, as you say.

Therefore, in .NET, it depends on your current "culture" (Java would call this a "locale", I guess).

It seems that if you want to do String comparison "à la Java" in .NET, you must use String.CompareOrdinal() instead.

On the opposite, if you want to do locale-dependent string comparison in Java, you need to use a Collator.

Lastly, another link on MSDN shows the influence of cultures on comparisons and even string equality.

fge
  • 119,121
  • 33
  • 254
  • 329
  • Thanks. I know the [String.CompareOrdinal()](http://msdn.microsoft.com/en-us/library/vstudio/system.string.compareordinal%28v=vs.90%29.aspx). In this question, i want to know How determines which capital letter is greater than small letter in .Net? Because i try the code: `String.Compare("A", "a", false, CultureInfo.GetCultureInfo("en-US"))`, the return value is 1. I can't understand... – smartleos Jun 28 '13 at 06:57
  • Uhmwell, that is probably culture-dependent. Try with French, for instance. I have no idea how cultures affect comparisons -- and I am quite surprised in fact that .NET behaves this way by default, it seems counterintuitive. – fge Jun 28 '13 at 06:58
  • Look at my edit. I guess .NET has the same thing as a `Collator` as well. – fge Jun 28 '13 at 07:02
2

From Java String

Returns: the value 0 if the argument string is equal to this string; a value less than 0 if this string is lexicographically less than the string argument; and a value greater than 0 if this string is lexicographically greater than the string argument.

From .Net String.CompareTo

This method performs a word (case-sensitive and culture-sensitive) comparison using the current culture. For more information about word, string, and ordinal sorts, see System.Globalization.CompareOptions.

This post explains the difference between the comparison types

And the doc explains the difference between all the comparison types;

IF you look at these two, CurrentCulture and Ordinal

 StringComparison.Ordinal: 
 LATIN SMALL LETTER I (U+0069) is less than LATIN SMALL LETTER DOTLESS I (U+0131) 
 LATIN SMALL LETTER I (U+0069) is greater than LATIN CAPITAL LETTER I (U+0049) 
 LATIN SMALL LETTER DOTLESS I (U+0131) is greater than LATIN CAPITAL LETTER I (U

 StringComparison.CurrentCulture: 
 LATIN SMALL LETTER I (U+0069) is less than LATIN SMALL LETTER DOTLESS I (U+0131) 
 LATIN SMALL LETTER I (U+0069) is less than LATIN CAPITAL LETTER I (U+0049) 
 LATIN SMALL LETTER DOTLESS I (U+0131) is greater than LATIN CAPITAL LETTER I (U+0049) 

Ordinal is the only one where "i" > "I" and hence Java like

Community
  • 1
  • 1
Java Devil
  • 10,629
  • 7
  • 33
  • 48
-1

This is due to the order of the characters in the ASCII character set. this is something you should really understand if you are going to do any form of data manipulation in your programs.

I am not sure if the grid control has any properties that allow you to modify the sort order, if not you will have to write your own sort subroutine.

You could use the std::sort function with a user defined predicate function that puts all lower case before upper case.

Manish Doshi
  • 1,205
  • 1
  • 9
  • 17