String.Compareto undocumented feature

Started by EnterTheStory (aka tolworthy), Tue 09/09/2008 17:43:04

Previous topic - Next topic

EnterTheStory (aka tolworthy)

http://www.adventuregamestudio.co.uk/yabb/index.php?topic=29505.0 This thread indicates that String.Compareto has an undocumented feature: it returns '-1' or '+1' depending on the relative length of strings. But another thread goes further:
Quote from: SteveMcCrea on Fri 06/01/2006 00:40:54
Try String.CompareTo(). I presume it's a wrapper for the C function strcmp, which returns 0 if the strings are the same, <0 if the first string should be first alphabetically, and >0 if the first string should be second alphabetically.

Can someone confirm? Thanks.

monkey0506

The official documented functionality is that if they're the same it returns 0 and it returns non-zero otherwise. But seeing as it was Chris who said it I'd be inclined to believe that it does in fact return -1 if the first string is first alphabetically or 1 if it's last.

Quote from: Pumaman on Thu 21/12/2006 22:03:34Actually, this is already the case with String.CompareTo, it's just not an official feature of the function. Try it out, it should already do what you want.

Pumaman

String.CompareTo returns a number less than 0 (not necessarily -1) if the string is first alphabetically, and a number greater than zero (not necessarily 1) if the other string is first alphabetically.

Bear in mind though that this is case sensitive, so although "A" vs "B" would return -1, "a" vs "B" would return 1.

GarageGothic

Thanks for pointing out the case sensitivity. I'm using it to sort listboxes and structs alphabetically and never experienced problems, but then all my Strings started with a capital letter. So I guess I should actually use "String.LowerCase()" inside the compare function to be on the safe side.

monkey0506

I imagine just passing false as the caseSensitive parameter would work quite the same as actually making both Strings the same case first...?

GarageGothic

Oh yeah, forgot about that parameter. Actually I am using that, it's just been a while since I wrote the sorting function so I forgot how it worked.

SMF spam blocked by CleanTalk