Home Tutorials Forums Articles Blogs Movies Library Employment Press
Old 06-27-2005, 05:25 AM   #1
calculus
Registered User
 
Join Date: Jun 2005
Location: New York
Posts: 9
Default Array: how do I sort multidimensional numeric array in MX?

I need some help on how to sort an array with nested arrays of matched numbers: one nested array holds x-coordinates and the other nested array holds y-coordinates. The position of each x,y pair is matched, so if I use a typical numerical sorting function, I succeed only in sorting one array or the other, and I undo the relationship.

I describe my arrays below with example x,y coords, and although I may be heading in the wrong direction, I included the function that I know works to sort either one or the other array:

[as]
mySort = function(a,b) {
return a-b;
}
var x= new Array(12,21,-13,48,33);
var y= new Array(6, -99, 7, -2, -2);
var xy = new Array(x,y)
xy.sort(mySort);
//output: -13,12,21,33,48,6,-99,7,-2,-2;
//(only first nested array of xy sorts, so nested arrays lose their relationship)
I need to output: -13,12,21,33,48,7,6,-99,-2,-2

/[as]
The arrays don't have to be nested. I would be happy to be able to sort the x array and have the y array be sorted by the x array.

Last edited by calculus; 06-27-2005 at 05:36 AM.
calculus is offline   Reply With Quote
Old 06-27-2005, 07:31 AM   #2
Xeef
Off-Line
 
Xeef's Avatar
 
Join Date: Aug 2004
Location: Ibiza/Spain language :Hungarian/German/ abit English
Posts: 6,539
Default

hi and welcome to As.Org

ActionScript Code:
XY = [{x:12, y:6}, {x:21, y:-99}, {x:-13, y:7}, {x:48, y:-2}, {x:33, y:-2}]; for (a=0; a<XY.length; a++) {     trace(XY[a].x+"  "+XY[a].y); } XY.sortOn("x"); trace("---After Sort---"); x=[] y=[] for (a=0; a<XY.length; a++) {     trace(XY[a].x+"  "+XY[a].y);     x.push(XY[a].x)     y.push(XY[a].y)     } xy=[x,y] trace(xy)
__________________

test your self -->http://www.actionscript.org/forums/s...86&postcount=8
Xeef is offline   Reply With Quote
Old 06-27-2005, 03:51 PM   #3
calculus
Registered User
 
Join Date: Jun 2005
Location: New York
Posts: 9
Default Arrays to nested arrays problem

Thanks, Xeef

I appreciate your help - having the nested array in that style did the trick!

Now I'm flummoxed as to how to convert my two simple arrays into the nested style that your solution starts with: XY = [{x:12, y:6}, etc.]

I tried several ways to construct the nested array style you used, and I feel I am close with the following, but no cigar:

ActionScript Code:
v = (12,21,-13,48,33); w = (6,-99,7,-2,-2); VW = [{}]; for (a=0; a<v.length; a++) {     VW[a].v.push(v[a]);     VW[a].w.push(w[a]); } for (a=0; a<VW.length; a++) {     trace(VW[a].v+"  "+VW[a].w); } //no output //I need the output: VW = [{v:12, w:6}, {v:21, w:-99}, {v:-13, w:7}, {v:48, w:-2}, {v:33, w:-2}];
calculus is offline   Reply With Quote
Reply


Thread Tools
Display Modes Rate This Thread
Rate This Thread:

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 Off
HTML code is Off

Forum Jump


All times are GMT. The time now is 07:08 AM.

///
Follow actionscriptorg on Twitter

 


Powered by vBulletin® Version 3.8.5
Copyright ©2000 - 2014, Jelsoft Enterprises Ltd.
Ad Management plugin by RedTyger
Copyright 2000-2013 ActionScript.org. All Rights Reserved.
Your use of this site is subject to our Privacy Policy and Terms of Use.