Adventure Game Studio

AGS Support => Beginners' Technical Questions => Topic started by: KamikazeHighland on Mon 16/01/2012 19:08:01

Title: Arranging Values in Order
Post by: KamikazeHighland on Mon 16/01/2012 19:08:01
I'm wondering if anyone's ever had to arrange ints, floats or strings in numerical, alphabetical or alphanumerical order?  And if so how did you do it?

Not the names of the variables but the actual values.

Edit:  I see how to do it with strings using CompareTo.  Any hints or tips are appreciated.
Title: Re: Arranging Values in Order
Post by: monkey0506 on Mon 16/01/2012 19:44:17
Well, without searching for any particular algorithm, you could do something like this for ints:

int[] SortIntArray(int arr[], int size)
{
  int i = 1;
  while (i < size)
  {
    int j = i;
    while ((j > 0) && (arr[j] < arr[j - 1]))
    {
      int t = arr[j];
      arr[j] = arr[j - 1];
      arr[j - 1] = t;
      j--;
    }
    i++;
  }
  return arr;
}


This actually modifies the original array...and returns it. You could just copy the data into a new array if you wanted. Works only with dynamic arrays.

Usage:

int arr[] = new int[5];
arr[0] = 5;
arr[1] = 4;
arr[2] = 3;
arr[3] = 2;
arr[4] = 1;
SortIntArray(arr, 5);


If you wanted it to work the other way (descending) then that would be a pretty easy change to the code I'm pretty sure...but I have to go and don't have time to test that. :P
Title: Re: Arranging Values in Order
Post by: Khris on Mon 16/01/2012 20:24:46
I had already typed & tested this when monkey posted so here goes:

// header

enum eOrder {
  eOrderAscending,
  eOrderDescending
};

import int[] SortInts(int a[],  int length, eOrder order = eOrderAscending);


// main script

int[] SortInts(int a[],  int length, eOrder order) {
  if (length < 2) return a;
  int i, j, swap;
  while (i < length - 1) {
    j = i + 1;
    while (j < length) {
       if ((order == eOrderAscending && a[i] > a[j]) ||
           (order == eOrderDescending && a[i] < a[j])) {
         swap = a[i];
         a[i] = a[j];
         a[j] = swap;
       }
       j++;
    }
    i++;
  }
  return a;
}


And here's the code I used to test this:

    // ten random ints in the range of 0 - 100
    int b[];
    b = new int[10];
    int i;
    while (i < 10) {
      b[i] = Random(100);
      i++;
    }

    // sort and display them
    b = SortInts(b, 10);   // must be reassigned to b in order to actually change b
    i = 0;
    while (i < 10) {
      Display("%d: %d", i, b[i]);
      i++;
    }


You can pretty much duplicate the function for floats; it's a bit trickier for Strings though.

bool ABeforeB(String a, String b) {
  if (a == b) return true;
  int l = a.Length;
  if (b.Length < l) l = b.Length;
  int i;
  while (i < l) {
    if (a.Chars[i] > b.Chars[i]) return false;
    i++;
  }
  return true;
}

Now use that to determine whether the Strings need to be swapped. (I didn't test that part though.)
Title: Re: Arranging Values in Order
Post by: monkey0506 on Mon 16/01/2012 23:33:21
Khris, String.CompareTo already handles the "ABeforeB" bit:

QuoteReturns 0 if the strings match, a number less than 0 if this string is earlier in the alphabet than STR2, and a number greater than 0 if this string is later in the alphabet than STR2.

CompareTo also allows you to toggle case-sensitivity. ;)

And I thought that inverting the comparison operator on the ints would work for sorting the other way, like I said, I just didn't have the chance to test it out to make sure (I don't write sort algorithms every day y'know!).

This thread should be pretty much solved now unless there's something we need to clarify.

P.S. Khris, are you aware that dynamic arrays are passed by reference, not value? ;)
Title: Re: Arranging Values in Order
Post by: KamikazeHighland on Tue 17/01/2012 01:11:38
Thanks!  I'm working on fitting this to what I need but this is pretty much solved.