Arranging Values in Order

Started by KamikazeHighland, Mon 16/01/2012 19:08:01

Previous topic - Next topic

KamikazeHighland

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.

monkey0506

Well, without searching for any particular algorithm, you could do something like this for ints:

Code: ags
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:

Code: ags
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

Khris

I had already typed & tested this when monkey posted so here goes:

Code: ags
// header

enum eOrder {
  eOrderAscending,
  eOrderDescending
};

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


Code: ags
// 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:
Code: ags

    // 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.

Code: ags
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.)

monkey0506

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? ;)

KamikazeHighland

Thanks!  I'm working on fitting this to what I need but this is pretty much solved.

SMF spam blocked by CleanTalk