Adventure Game Studio

AGS Support => Advanced Technical Forum => Topic started by: EnterTheStory (aka tolworthy) on Tue 09/09/2008 17:43:04

Title: String.Compareto undocumented feature
Post by: EnterTheStory (aka tolworthy) on Tue 09/09/2008 17:43:04
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.
Title: Re: String.Compareto undocumented feature
Post by: monkey0506 on Tue 09/09/2008 19:35:12
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 (http://www.adventuregamestudio.co.uk/yabb/index.php?topic=29505.msg376560#msg376560) 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.
Title: Re: String.Compareto undocumented feature
Post by: Pumaman on Wed 10/09/2008 18:42:48
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.
Title: Re: String.Compareto undocumented feature
Post by: GarageGothic on Wed 10/09/2008 18:47:29
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.
Title: Re: String.Compareto undocumented feature
Post by: monkey0506 on Wed 10/09/2008 19:40:48
I imagine just passing false as the caseSensitive parameter would work quite the same as actually making both Strings the same case first...?
Title: Re: String.Compareto undocumented feature
Post by: GarageGothic on Wed 10/09/2008 19:42:59
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.