It would be great if String.CompareTo could be modified to return:
0 if strings equal
-1 if string A<B
1 if string b>A
as currently, sorting strings alphabetically, you have to do it char by char, which is a pain. It would be like the <=> operator in some languages...
Jesus christ SSH, you're the module master, and instead of showing off with yet another module, you actually ask CJ for improved functionality? What has the world come to? ;)
I think this snippet of code does pretty much what you're requesting, it doesn't currently distringuish by upper- and lowercase though:
#sectionstart Alphabetize(String a, String b)// DO NOT EDIT OR REMOVE THIS LINE
function Alphabetize(String a, String b) {
int ordered;
int charlength;
if (a.Length >= b.Length) charlength = b.Length;
else if (a.Length < b.Length) charlength = a.Length;
int lettercount;
while ((ordered == 0) && (lettercount <= charlength)) {
if (a.Chars[lettercount] > b.Chars[lettercount]) ordered = -1;
else if (a.Chars[lettercount] < b.Chars[lettercount]) ordered = 1;
else lettercount++;
}
return ordered;
}
#sectionend Alphabetize(String a, String b)// DO NOT EDIT OR REMOVE THIS LINE
Oh, I've got it already:
function strcmp(String a, String b) {
int i=0;
while (i<a.Length) {
if (i>=b.Length) return 1;
else if (a.Chars[i]<b.Chars[i]) return -1;
else if (a.Chars[i]>b.Chars[i]) return 1;
i++;
}
if (i<b.Length) return -1;
else return 0;
}
I just thought a native one would go a lot faster... using it for my "find position in ordered list"...
Is it actually slow though? I order a very large array using that function and don't see any framerate drops.
Actually, 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.
D'oh!
Andrew you're slipping up. You should test that things don't already work that way before you go saying they need to be fixed. :P
Oh, don't be so hard on dear SSH. Even he has the right to make mistakes.
Oy, it's already possible? That fact would have come in very handy when I had to manually code a messy way of making new entries in ListBoxes be alphabetically rearranged! :'( Could you please put it in the manual?
Yeah, I'm doing a similar thing, Rui. My binary search/insert code:
function Globals::find(String ss) {
if (this.last!=null && ss==this.last) return this.lasti;
this.last=ss;
int left=0;
int right=this.count-1;
while (left <= right) {
int mid = (left+right)/2;
int cmp = ss.CompareTo(this.name[mid], true);
if (cmp>0)
left = mid+1;
else if (cmp<0)
right = mid-1;
else {
this.lasti=mid;
return mid;
}
}
// No match, then insert
int i=this.count;
this.count++;
while (i>left) {
int j=i-1;
this.name[i]=this.name[j];
this.val[i]=this.val[j];
i=j;
}
this.name[left]=ss;
this.val[left]=0;
this.str[left]="";
this.lasti=left;
return left;
}
Heh, wish I could code as elegantly/simply as that. My own code is extremely messy. It works, but damn it's messy.
Yeah well, you might notice a certain similarity in my code to the second algorithm here: http://en.wikipedia.org/wiki/Binary_search :=