Adventure Game Studio

AGS Support => Beginners' Technical Questions => Topic started by: Technocrat on Tue 28/05/2013 10:10:52

Title: [Fixed] Sorting lists...
Post by: Technocrat on Tue 28/05/2013 10:10:52
I've got a listbox, designed to contain useful topics learned throughout a game. My intention is to add new topics to it as they become relevant, but from what I can tell, they will only ever be arranged in the order that they've been added. Is there a way to sort a ListBox's contents by some criteria (e.g. Alphabetical, string length, etc), or is it just wishful thinking?

It doesn't look immediately possible to me, but I thought I might as well ask just in case someone knew something I didn't.
Title: Re: Sorting lists...
Post by: Stupot on Tue 28/05/2013 14:05:08
Could this help? It's not quite what you asked for but you might be able to manipulate it to get them in the order you want?
http://www.adventuregamestudio.co.uk/manual/ags62.htm#listbox.insertitemat
Title: Re: Sorting lists...
Post by: Khris on Tue 28/05/2013 15:00:00
Here you go:

header:
Code (ags) Select
enum eOrder {
  eOrderAscending,
  eOrderDescending
};

import void Sort(this ListBox*, eOrder order = 1, bool caseSensitive = false);


script:
Code (ags) Select
void Sort(this ListBox*, eOrder order, bool caseSensitive) {
 
  int ic = this.ItemCount;
  if (ic < 2) return;

  int i, j, c;
  String temp;
  while (i < ic - 1) {
    j = i + 1;
    while (j < ic) {
      c = this.Items[i].CompareTo(this.Items[j], caseSensitive);
      if ((c < 0 && order == eOrderDescending) || (c > 0 && order == eOrderAscending)) {
        temp = this.Items[i];
        this.Items[i] = this.Items[j];
        this.Items[j] = temp;
      }
      j++;
    }
    i++;
  }
}


Both parameters are optional; default sorting is ascending while ignoring case.
Title: Re: Sorting lists...
Post by: Technocrat on Wed 29/05/2013 13:45:25
Ah, fantastic! That suits what I'm aiming for perfectly. Merci beaucoup!
Title: Re: Sorting lists...
Post by: Technocrat on Thu 06/06/2013 12:26:54
Well, I've finally gotten around to giving the script a go, but I keep hitting an issue.

Every time I try to use the script, it complains. In this case, the output says Error (line 189): Undefined token 'Sort'. From what I've read elsewhere on here, I'm fairly certain this is an issue of where to place the script, though the issue has happened both when it was at the top of GlobalScript, and in a new script as well.

Also, programming noob question - why is it 'void' here, rather than 'function'?

Title: Re: Sorting lists...
Post by: Khris on Thu 06/06/2013 14:30:27
I probably should have mentioned this:
The code I posted creates an extender function. This means you don't call it using Sort(lbMyListbox); but instead you call: lbMyListbox.Sort();

Regarding void: in strictly typed languages like C or Java, each function has to specify what it returns. A function what returns a String would have String instead of function in front of its name. The word function is just a substitute for int. With AGS, it's fine to specify a return type but not actually return anything, so using function is fine as long as you are returning an int or nothing. But, the proper way of specifying that the function isn't going to return a value is using void.
Title: Re: Sorting lists...
Post by: Technocrat on Fri 07/06/2013 11:00:48
Thank you! That works delightfully well. Now to go and organise some lists...