Game Development Community

Sort data from Multi Object ArrayList by Int ASC [SOLVED]

by Aaron Scovel · in Torque X 2D · 09/28/2010 (12:36 am) · 5 replies

My ArrayList is dynamically created when a scene is loaded via components added to objects.

public void AddOption(int option, Vector2 position)
        {
            _options.Add(new OptionRow(option,position));
        }
www.topnotched.com/images/ArrayList.png
(I have many rows in the arraylist now, this image is for example/debug info)

Here is the OptionRow class:
class OptionRow
    {
        public int newOption;
        public Vector2 newPosition;

        public OptionRow(int o, Vector2 p)
        {
            newOption = o;
            newPosition = p;
        }
    }

The ArrayList is created in the same order as the objects are added to the scene, this can lead to the "option" being out of order. I am trying to sort _options by the "option" int in ASC order.

I have searched the net for a couple hours and cant find a solution (I would assume its something real simple)

I will give "code help credit" in the text when the recourse is completed :-)

Best Regards,

About the author

Previously a PHP/MySQL Programmer/Web Developer of 10 years. In Aug 2010 I decided to change careers, and this is were I landed! I also parent 3 kids full time. TopNotched.com


#1
09/28/2010 (12:47 am)
You should try a bubble sort. It may be the one of the slowest sorting algarithms out there, but to start off with its not bad.

// array of integers to hold values
private int[] a = new int[100];

// number of elements in array
private int x;

// Bubble Sort Algorithm
public void sortArray()
{
  int i;
  int j;
  int temp;

  for( i = (x - 1); i >= 0; i-- )
  {
    for( j = 1; j <= i; j++ )
    {
      if( a[j-1] > a[j] )
      {
        temp = a[j-1];
        a[j-1] = a[j];
        a[j] = temp;
      }
    }
  }
}

Bubble Sort

Besides being slow, if you are only calling it once, it is a very good sort to start with. Later on you can try fancier stuff, but this should get the job done and put you on your merry way.

-Will

#2
09/28/2010 (3:25 am)
Thanks for the quick reply Will

Is there any easy way to convert that into an Icomparer to be used with the arraylist? like:
_options.Sort(bubblesort);

I finally ran across something that might be perfect for what I need, this is almost exactly like my setup except im using int, vector2 instead of int, string. Sort Object ArrayList in C# I will try to code it up and let you all know if its a success.

Why did they make this so complicated in C#, you can do the same thing with MySQL/PHP with about 2 lines of code.
#3
09/28/2010 (4:19 am)
Why don't you just extend List, and use bubble sort as a function...Thats one idea;
#4
09/28/2010 (5:45 am)
This is the solution I ended up with, if anyone is interested.

class OptionRow
    {
        public int newOption;
        public Vector2 newPosition;

        public OptionRow(int o, Vector2 p)
        {
            newOption = o;
            newPosition = p;
        }
        
        public int CompareTo(OptionRow opt2, ObjCompare.ComparisonType comparisonType, OptionRow opt1)
        { 
            int returnValue;
            if (comparisonType == ObjCompare.ComparisonType.newOption)
            {
                returnValue = newOption.CompareTo(opt2.newOption); 
            }
            else 
            {
                returnValue = newOption.CompareTo(opt2.newOption); 
            } 
            return returnValue; 
        } 
    }

    // Create IComparer to Sort Options
    class ObjCompare : IComparer 
    {
        public enum ComparisonType
        {
            newOption, newPosition
        }
        private ComparisonType compMethod;
        public ComparisonType ComparisonMethod
        { 
            get { return compMethod; }
            set { compMethod = value; }
        } public int Compare(object x, object y)
        {
            OptionRow opt1 = (OptionRow)x;
            OptionRow opt2 = (OptionRow)y;
            return opt1.CompareTo(opt2, ComparisonMethod, opt1);
        }
    }

I then decided to make a new method to handle the sorting and called SortArray

//This method is called via components to add new options
        public void AddOption(int option, Vector2 position)
        {
            // Add new options to the array
            _options.Add(new OptionRow(option,position));
            // Sort the array as new options are added
            SortArray();
        }

        public void SortArray()
        {
            ObjCompare objcom = new ObjCompare();
            objcom.ComparisonMethod = ObjCompare.ComparisonType.newOption;
            _options.Sort(objcom);
            
        }

Result: Multi Object Array _options is now sorted by "newOption" Int Asc
#5
09/28/2010 (7:09 am)
Thats some good looking code.. This could be a useful resource!