Graal Forums  

Go Back   Graal Forums > Development Forums > NPC Scripting
FAQ Members List Calendar Today's Posts

Reply
 
Thread Tools Search this Thread Display Modes
  #1  
Old 02-05-2007, 11:49 PM
Twizt3d Twizt3d is offline
Registered User
Join Date: Jul 2003
Posts: 15
Twizt3d is on a distinguished road
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?
Reply With Quote
  #2  
Old 02-05-2007, 11:58 PM
Inverness Inverness is offline
Incubator
Inverness's Avatar
Join Date: Aug 2004
Location: Houston, Texas
Posts: 3,613
Inverness is a jewel in the roughInverness is a jewel in the rough
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
__________________
Reply With Quote
  #3  
Old 02-06-2007, 12:11 AM
Twizt3d Twizt3d is offline
Registered User
Join Date: Jul 2003
Posts: 15
Twizt3d is on a distinguished road
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"}; 
Reply With Quote
  #4  
Old 02-06-2007, 12:19 AM
Inverness Inverness is offline
Incubator
Inverness's Avatar
Join Date: Aug 2004
Location: Houston, Texas
Posts: 3,613
Inverness is a jewel in the roughInverness is a jewel in the rough
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.
__________________

Last edited by Inverness; 02-06-2007 at 12:33 AM..
Reply With Quote
  #5  
Old 02-06-2007, 12:41 AM
Twizt3d Twizt3d is offline
Registered User
Join Date: Jul 2003
Posts: 15
Twizt3d is on a distinguished road
Ill give it a shot Thanks a bunch
Reply With Quote
  #6  
Old 02-06-2007, 12:45 AM
Inverness Inverness is offline
Incubator
Inverness's Avatar
Join Date: Aug 2004
Location: Houston, Texas
Posts: 3,613
Inverness is a jewel in the roughInverness is a jewel in the rough
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.
__________________
Reply With Quote
  #7  
Old 02-06-2007, 03:53 AM
Twizt3d Twizt3d is offline
Registered User
Join Date: Jul 2003
Posts: 15
Twizt3d is on a distinguished road
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
Reply With Quote
  #8  
Old 02-06-2007, 04:48 AM
Tolnaftate2004 Tolnaftate2004 is offline
penguin.
Join Date: Jul 2004
Location: Berkeley, CA
Posts: 534
Tolnaftate2004 is a jewel in the roughTolnaftate2004 is a jewel in the rough
Send a message via AIM to Tolnaftate2004
Quote:
Originally Posted by Inverness View Post
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);
  }

__________________
◕‿‿◕ · pfa · check yer syntax! · src

Killa Be: when i got that locker in 6th grade the only thing in it was a picture of a midget useing a firehose :/
Reply With Quote
  #9  
Old 02-06-2007, 04:56 AM
Twizt3d Twizt3d is offline
Registered User
Join Date: Jul 2003
Posts: 15
Twizt3d is on a distinguished road
o.O
Reply With Quote
  #10  
Old 02-06-2007, 05:29 AM
Tolnaftate2004 Tolnaftate2004 is offline
penguin.
Join Date: Jul 2004
Location: Berkeley, CA
Posts: 534
Tolnaftate2004 is a jewel in the roughTolnaftate2004 is a jewel in the rough
Send a message via AIM to Tolnaftate2004
Just make a copy and delete in a random order until you have 2 or 7 or whatever.
__________________
◕‿‿◕ · pfa · check yer syntax! · src

Killa Be: when i got that locker in 6th grade the only thing in it was a picture of a midget useing a firehose :/
Reply With Quote
  #11  
Old 02-06-2007, 05:31 AM
Twizt3d Twizt3d is offline
Registered User
Join Date: Jul 2003
Posts: 15
Twizt3d is on a distinguished road
how would that sort an array?
Reply With Quote
  #12  
Old 02-06-2007, 05:42 AM
Tolnaftate2004 Tolnaftate2004 is offline
penguin.
Join Date: Jul 2004
Location: Berkeley, CA
Posts: 534
Tolnaftate2004 is a jewel in the roughTolnaftate2004 is a jewel in the rough
Send a message via AIM to Tolnaftate2004
Quote:
Originally Posted by Twizt3d View Post
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.
__________________
◕‿‿◕ · pfa · check yer syntax! · src

Killa Be: when i got that locker in 6th grade the only thing in it was a picture of a midget useing a firehose :/
Reply With Quote
  #13  
Old 02-06-2007, 09:06 AM
Chandler Chandler is offline
Banned
Join Date: Jan 2007
Posts: 656
Chandler will become famous soon enough
Use
HTML Code:
object.insert(index, value);
Reply With Quote
  #14  
Old 02-07-2007, 02:02 AM
Twizt3d Twizt3d is offline
Registered User
Join Date: Jul 2003
Posts: 15
Twizt3d is on a distinguished road
Yes, I could use that but the second array is shorter then the original array(the order to sort the second array)
Reply With Quote
Reply


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +2. The time now is 01:59 PM.


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