View Full Version : Sorting array

Twizt3d

02-05-2007, 11:49 PM

I am adding values from one array into another array. How would I sort the second array by the index numbers of the first.

example:

this.firstarray = {1,2,3,4,5,6,7,8,9,10,11,12,13};

for (temp.i = 0; temp.i<7; temp.i++){

temp.random = int(random(0,13));

this.secondarray.add(this.firstarray[temp.random]);

//some algorthm to sort the second array by the index's of the first array

}

Does this make any sense?

Inverness

02-05-2007, 11:58 PM

Eh you should be a little more detailed in your explanation, I'm not quite catching it.

And does this help at all?

sortascending() - sorts the array in ascending order (smallest value first)

sortbyvalue(str, str, bool) - sorts the array by the specified variable name, you must also say what variable type it is (e.g. string) and if it should be sorted ascending

sortdescending() - sorts the array in descending order (highest value first)

Twizt3d

02-06-2007, 12:11 AM

ok Ill give some more detail:

this.firstarray = {"One","Two","Three","Four","Five","Six","Seven","Eight","Nine","Ten","Eleven","Twelve","Thirteen"};

for (temp.i = 0; temp.i<7; temp.i++){

temp.random = int(random(0,13));

this.secondarray.add(this.firstarray[temp.random]);

//some algorthm to sort the second array by the index's of the first array

}

this.secondarray will contain a random list of this.firstarray[] values. How could I put them in order. Example:

this.secondarray = {"Two","Eleven","Three","Five","One","Twelve","Nine"};

Lets say the outcome of the for() made this.secondarray equal that.

How could I put this.secondarray in order from lowest to highest? So it will look like this:

this.secondarray = {"One","Two","Three","Five","Nine","Eleven","Twelve"};

Inverness

02-06-2007, 12:19 AM

Well you would need an array of their correct positions obviously. Then make a second array in the function which will be the one you're going to return. Then use a for loop on the first array, and if you find that element in second array add it to the array you're going to return. It would end up in correct order.

function onCreated() {

this.firstarray = {"One","Two","Three","Four","Five","Six","Seven","Eight","Nine","Ten"};

this.secondarray = sort({"Three","Nine","Five","Four"});

}

function sort(inarray) {

temp.outarray;

temp.i;

temp.e;

temp.c = inarray.size();

temp.sortarray = this.firstarray;

for (i = 0; i < sortarray.size() && c > 0; i ++) {

e = sortarray[i];

if (inarray.index(e) > -1) {

outarray.add(e);

c--;

}

}

return outarray;

}

That should be the simplest version.

Twizt3d

02-06-2007, 12:41 AM

Ill give it a shot Thanks a bunch

Inverness

02-06-2007, 12:45 AM

I feel like I made a mistake >_>

Or like I'm forgetting something.

I never did test to see if that works.

Edit: Just tested, it works. :)

Though that script would only work for that exact situation or something really similar. Its not exactly customizable and I'm too lazy to do anything more.

Twizt3d

02-06-2007, 03:53 AM

heh well my situation is a little more complicated so I will take that and see if I can use it. Really I am doing: temp.array.add(temp.array1[i]@"-"@temp.array2[i]); and trying to sort temp.array using temp.array1's values

Tolnaftate2004

02-06-2007, 04:48 AM

That should be the simplest version.

function onCreated() {

this.firstarray={"A","B","C",...,"Z"};

temp.len = this.firstarray.size();

temp.take = 2; /* how many to take from first array */

this.secondarray = this.firstarray;

for (temp.i=0;temp.i<temp.len-temp.take;temp.i++){

temp.random = int(random(0,temp.len-temp.i-.0000001));

this.secondarray.delete(temp.random);

}

}

:confused:

Twizt3d

02-06-2007, 04:56 AM

o.O

Tolnaftate2004

02-06-2007, 05:29 AM

Just make a copy and delete in a random order until you have 2 or 7 or whatever.

Twizt3d

02-06-2007, 05:31 AM

how would that sort an array?

Tolnaftate2004

02-06-2007, 05:42 AM

how would that sort an array?

It looked to me that all you were doing was taking select pieces from an array - and wanted them to retain their order.

Chandler

02-06-2007, 09:06 AM

Use

object.insert(index, value);

Twizt3d

02-07-2007, 02:02 AM

Yes, I could use that but the second array is shorter then the original array(the order to sort the second array)

vBulletin® v3.8.11, Copyright ©2000-2019, vBulletin Solutions Inc.