Adventure Game Studio

AGS Support => Advanced Technical Forum => Topic started by: SSH on Thu 21/12/2006 12:04:39

Title: String.CompareTo improvements (SOLVED)
Post by: SSH on Thu 21/12/2006 12:04:39
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...


Title: Re: SUGEGSTION: String.CompareTo improvements
Post by: GarageGothic on Thu 21/12/2006 13:16:17
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
Title: Re: SUGGESTION: String.CompareTo improvements
Post by: SSH on Thu 21/12/2006 14:37:24
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"...

Title: Re: SUGGESTION: String.CompareTo improvements
Post by: GarageGothic on Thu 21/12/2006 14:47:29
Is it actually slow though? I order a very large array using that function and don't see any framerate drops.
Title: Re: SUGGESTION: String.CompareTo improvements
Post by: Pumaman on Thu 21/12/2006 22:03:34
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.
Title: Re: SUGGESTION: String.CompareTo improvements
Post by: SSH on Fri 22/12/2006 06:28:18
D'oh!
Title: Re: SUGGESTION: String.CompareTo improvements
Post by: monkey0506 on Fri 22/12/2006 06:29:43
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
Title: Re: SUGGESTION: String.CompareTo improvements
Post by: fovmester on Fri 22/12/2006 07:23:21
Oh, don't be so hard on dear SSH. Even he has the right to make mistakes.
Title: Re: SUGGESTION: String.CompareTo improvements
Post by: Rui 'Trovatore' Pires on Fri 22/12/2006 08:36:32
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?
Title: Re: SUGGESTION: String.CompareTo improvements
Post by: SSH on Fri 22/12/2006 08:45:47
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;
}



Title: Re: SUGGESTION: String.CompareTo improvements
Post by: Rui 'Trovatore' Pires on Fri 22/12/2006 08:58:40
Heh, wish I could code as elegantly/simply as that. My own code is extremely messy. It works, but damn it's messy.
Title: Re: SUGGESTION: String.CompareTo improvements
Post by: SSH on Fri 22/12/2006 09:18:15
Yeah well, you might notice a certain similarity in my code to the second algorithm here: http://en.wikipedia.org/wiki/Binary_search  :=