Adventure Game Studio

AGS Support => Beginners' Technical Questions => Topic started by: bulka_tarta on Sat 25/02/2017 20:45:47

Title: Sorting items in ListBoxes not working as intended. [SOLVED]
Post by: bulka_tarta on Sat 25/02/2017 20:45:47
Hi,

I have a ListBox that I wanted to be in alphabetical order - and I have manged to find piece of code on these forums to help me achieve this. It works great! Well... almost - there's a little catch to it thought.

I have bunch of arrays that I want to display on GUI labels when a certain item in the ListBox is clicked on. The problem is that these arrays are meant to be connected with each other - so "0" goes with all other "0" variables, "1" with other "1" and so on. When I sort the items in ListBox, it only sorts the "clientName[]", but all the other labels use the same Index as they are written in.

Code (ags) Select

// Creates list of clients in the lstDatabase
function ClientList()
{
ClientInfo(); // this function includes bunch of Arrays with required info

int index = 0;
   while (index < maxClients) { // maxClients = total number of clients in database
     lstDatabase.AddItem(clientName[index]);
     index ++;
   }   
  lstDatabase.Sort(eOrderAscending, false); //Sorting function
}

// Replaces all GUI labels and buttons with client info
function ClientLabels()
{
  lblName.Text = clientName[lstDatabase.SelectedIndex];
  lblAddress.Text = clientAddress[lstDatabase.SelectedIndex];
  lblName2.Text = clientName[lstDatabase.SelectedIndex];
  lblAddress2.Text = clientAddress[lstDatabase.SelectedIndex];
  bPhoto.NormalGraphic = clientPhoto[lstDatabase.SelectedIndex];
  bPhoto2.NormalGraphic = clientPhoto[lstDatabase.SelectedIndex];
}


Sort ListBox Code:
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++;
  }
}


What all the code above does is that it leaves me with "clientName[]" sorted alphabetically in the ListBox, but the labels just follow the order in which they are written in. I'm not sure how to get the labels to "stay" with their associated variables. Any help would be greatly appreciated.
Title: Re: Sorting items in ListBoxes not working as intended.
Post by: dayowlron on Sat 25/02/2017 23:04:57
The issue is it is just sorting the items that exist in the listbox and not the arrays. Unfortunately the ListBox control in AGS does not have an ItemData property. For this reason you need to sort all of the arrays as you sort the listbox.
It looks like you have 3 fields that are linked together. clientName, clientAddress, and clientPhoto. What you need to do is add a "swap" for each item you swap on the Listbox. If you use the "Sort" function for another list box then make a different function than this one because this one will be built for explicitly your arrays.

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

  int i, j, c, tempint;
  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;
        temp = clientName[i];
        clientName[i] = clientName[j];
        clientName[j] = temp;
        temp = clientAddress[i];
        clientAddress[i] = clientAddress[j];
        clientAddress[j] = temp;
        tempint = clientPhoto[i];
        clientPhoto[i] = clientPhoto[j];
        clientPhoto[j] = tempint;
      }
      j++;
    }
    i++;
  }
}

Title: Re: Sorting items in ListBoxes not working as intended. [SOLVED]
Post by: bulka_tarta on Sun 26/02/2017 07:57:18
This works great! Thank you very much for your help!
I intend to add a few more arrays, but I imagine I can just simply add them in into the code.