00:00
00:00
Newgrounds Background Image Theme

Mekatov just joined the crew!

We need you on the team, too.

Support Newgrounds and get tons of perks for just $2.99!

Create a Free Account and then..

Become a Supporter!

Dynamic Sorting

1,433 Views | 13 Replies
New Topic Respond to this Topic

Dynamic Sorting 2005-05-28 03:29:53


How do I order an X amount of Numbers in different Dynamic Text Boxes so say there are 5 the numbers are

2
3.1
1
0.6
4.5

how would I make them order from largest to smallest, then the largest (4.5) would appear in a dynamic text box labelled "top" and the lowest in "bottom"

Cheers

Response to Dynamic Sorting 2005-05-28 04:51:47


anyone know a solution?

Response to Dynamic Sorting 2005-05-28 04:53:45


I do not know sorry. Have a look at Ultimate Tutorial 2. It always has the answer.

Response to Dynamic Sorting 2005-05-28 05:29:22


At 5/28/05 04:53 AM, AlienHeadX wrote: I do not know sorry. Have a look at Ultimate Tutorial 2. It always has the answer.

Not for this it doesnt

Response to Dynamic Sorting 2005-05-28 06:30:34


You can use a sorting algorithm. This one is not the fastest, but it's easy to understand.


//Create array to hold all values
num = new Array;
//Give the array the text boxes values
/*You will have to replace the values with the variables of the dynamic text boxes*/
num[1] = 2;
num[2] = 3.1;
num[3] = 1;
num[4] = 4.5;
num[5] = 0.6;
//number of text boxes
numBoxes = 5;
//Sort the values
for(x=1;x<numBoxes; x++) {
for(y=x+1;y<=numBoxes;y++) {
if(num[x] > num[y]) {
tmp=num[x];
num[x]=num[y];
num[y]=tmp;
}
}
}
//Assaign the highest value to the top text box
top.text = num[5];
//Assaign the lowest value to the bottom text box
bottom.text = num[1];
/*Just for displaying the sorted values to make sure that everything works*/
for(x=1;x<=numBoxes;x++) trace(num[x]);

Response to Dynamic Sorting 2005-05-28 07:01:53


At 5/28/05 06:30 AM, Xelius wrote: Array

Haha, I found this an interesting challenge, so I've been playing with arrays too. This requires the textboxes to have the VARs 'txt1', 'txt2', 'txt3' etc

on(release){
NumOfBoxes=5; //Number of textboxes
holdarray=new Array();
for(i=1;i<(NumOfBoxes+1);i++){
holdarray.push({numb: Number(_root["txt"+i])});
}
holdarray.sortOn("numb", 18);
for(i=0;i<NumOfBoxes;i++){
_root["txt"+(i+1)] = holdarray[i].numb;
}
}

Obviously this uses a button for the sort, but would work equally well on a frame


- - Flash - Music - Images - -

BBS Signature

Response to Dynamic Sorting 2005-05-28 07:10:26


At 5/28/05 07:01 AM, Denvish wrote: holdarray.sortOn("numb", 18);

I didn't know that Flash had its own sorting function, that's cheating xD

Response to Dynamic Sorting 2005-05-28 07:18:15


At 5/28/05 07:10 AM, Xelius wrote: I didn't know that Flash had its own sorting function

Neither did I until about 30 minutes ago... =)


- - Flash - Music - Images - -

BBS Signature

Response to Dynamic Sorting 2005-05-28 07:45:06


So if im getting these numbers im using a random function, and trying to determine the highest and lowest
How do I make sure they go to txt1 txt 2 etc when they come up as the random thing.

Response to Dynamic Sorting 2005-05-28 07:46:10


why sortOn? you could just use Array.Sort() , isn't it there?

Response to Dynamic Sorting 2005-05-28 08:26:27


At 5/28/05 07:46 AM, Inglor wrote: why sortOn?

Why not? It does the job.

At 5/28/05 07:45 AM, PelvicThrusters wrote: So if im getting these numbers im using a random function, and trying to determine the highest and lowest
How do I make sure they go to txt1 txt 2 etc when they come up as the random thing.

Please post the code you are using to create the numbers


- - Flash - Music - Images - -

BBS Signature

Response to Dynamic Sorting 2005-05-28 08:48:20


_root.a=(_root.b+_root.c)/20;;
if (_root.a>=5){
_root.a=value = int(Math.random(5)*1000)/200;

Response to Dynamic Sorting 2005-05-28 09:01:56


Declare this somewhere before you start generating numbers

_root.holdarray=new Array();

Add this line after the code you posted

_root.holdarray.push({numb: Number(_root.a)});

Use this when you want to transfer the array to textboxes

NumOfBoxes=5; //Number of textboxes
_root.holdarray.sortOn("numb", 18);
for(i=0;i<NumOfBoxes;i++){
_root["txt"+(i+1)] = _root.holdarray[i].numb;
}

This assumes that your text boxes have the VARs 'txt1', 'txt2', 'txt3' etc, and that they are on the _root.


- - Flash - Music - Images - -

BBS Signature

Response to Dynamic Sorting 2005-05-28 09:06:07


Denvish, without you Eviludy and Highroller, i would never have been able to make a game.
Yes people they are to blame... sick em