Click here to Skip to main content
15,887,083 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
"01:00".CompareTo("-1:00")

That is supposed to be 1, but it's -1 in C#.

Can someone tell me why? Otherwise I can't trust to use them at all.
Posted
Comments
Sergey Alexandrovich Kryukov 28-Sep-11 12:46pm    
Yes, I tested it... How did you want to use it?
--SA

1 solution

You're doing a string comparison. String comparisons are done in 'word sort order', which is a culture dependent thing and hyphens are ignored. See MSDN[^].

You probably wanted a numeric or DateTime comparison. String comparisons are not generally a good idea.
 
Share this answer
 
Comments
tryharder 28-Sep-11 13:54pm    
Thank you very much for the answer. (I sort expected that) It's in en-US culture. How can I turn on '-' for comparison in en-US? ( I never expected '-' is ignored in en-US.
tryharder 28-Sep-11 14:09pm    
I solved it by using CompareOptions.StringSort option by using string.Compare function. But the option does not seem to imply such action.(that MS, their English is kind bad)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900