Graal Forums

Graal Forums (https://forums.graalonline.com/forums/index.php)
-   NPC Scripting (https://forums.graalonline.com/forums/forumdisplay.php?f=8)
-   -   Sorting array (https://forums.graalonline.com/forums/showthread.php?t=71991)

Twizt3d 02-05-2007 11:49 PM

Sorting array
 
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:
PHP Code:

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

for (
temp.0temp.i<7temp.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?
PHP Code:

sortascending() - sorts the array in ascending order (smallest value first)
sortbyvalue(strstrbool) - sorts the array by the specified variable nameyou must also say what variable type it is (e.gstring) 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:

PHP Code:

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

for (
temp.0temp.i<7temp.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:

PHP Code:

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

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.

PHP Code:

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.inarray.size();
  
temp.sortarray this.firstarray;
  for (
0sortarray.size() && 0++) {
    
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

Quote:

Originally Posted by Inverness (Post 1273785)
That should be the simplest version.

PHP Code:

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

Quote:

Originally Posted by Twizt3d (Post 1273876)
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
HTML Code:

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)


All times are GMT +2. The time now is 09:07 AM.

Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2025, vBulletin Solutions Inc.
Copyright (C) 1998-2019 Toonslab All Rights Reserved.