Link to home
Start Free TrialLog in
Avatar of digitaldavenyc
digitaldavenyc

asked on

problem sorting an Array via "sortOn" ...

I am trying to sort an array that is generated thru an xml file.  

The XML has "sections" that are used to generate a different view type, it is NOT used here but is needed for other features.  What IS used is the artist name & ID.  This function takes the data from XML and takes the name and ID from each section and puts it into 2 separate Arrays, "nameArr" and "noArr".  After these two new arrays are created they are sent into an object "sectionData". The objects are then sent into a new Array "multiArr".  This looks a little overkill but it is needed to create a new array without the section info.

I am trying to sort this new array multiArr using the "sortOn" method by "ID" to sort it by id but it is not working.  It doesn't sort the array. please help!
public function setListData( data:Array ) {
 
	var sectionData:Object = {};
	var nameArr:Array = [];
	var noArr:Array = [];
	var multiArr:Array = [];
 
	for( var i:Number=0; i<data.length; i++ ) {
 
		for( var j:Number=0; j<data[i].artist.length; j++ ) {
			nameArr.push(data[i].artist[j].name);
			noArr.push(data[i].artist[j].id);
		}
 
	}
 
	sectionData.name = nameArr;
	sectionData.id = noArr;
	multiArr.name = sectionData.name;
	multiArr.id = sectionData.id;
 
	//sort array
	multiArr.sortOn('id', Array.NUMERIC);
 
	for ( var k:Number=0; k<multiArr.name.length; k++ ) {
		trace(multiArr.name[k] + " " + multiArr.id[k]);
	}
 
}
 
XML
<section>
	<artist name="Jeremyville" id="30"></artist>
	<artist name="Billie Joe Armstrong" id="10"></artist>
	<artist name="Megan Whitmarsh" id="14"></artist>
</section>
<section>
	<artist name="Noemi Riebesell" id="32"></artist>
	<artist name="Hiroshi Fujiwara" id="75"></artist>
	<artist name="Undefeated" id="70"></artist>
</section>
<section>
	<artist name="Ryan Lombardi" id="16"></artist>		
	<artist name="Steve Monti" id="58"></artist>
	<artist name="Mister Never" id="43"></artist>
</section>

Open in new window

Avatar of scooby_56
scooby_56
Flag of United Kingdom of Great Britain and Northern Ireland image

try converting to number when pushing into the array inside the loop

ie replace line 12 in your example




noArr.push( parseInt(data[i].artist[j].id) );

Open in new window

Avatar of digitaldavenyc
digitaldavenyc

ASKER

That did not fix the problem.  It was tracing as a string.  I added parseInt to the loop and it is still not sorting with the sortOn "id".
k...

for an array of objects you would use sort on... i.e

var array:Array = [ {id:2,aName:"ctesting"},{id:1,aName:"atesting"},{id:8,aName:"utesting"},{id:4,aName:"qtesting"},{id:6,aName:"btesting"}]
array.sortOn("id");
trace(array[0].aName); //traces atesting - so is sorting

you are using array of numeric values.. so use sort... i.e
var array:Array = [5,2,6,4]
array.sort()
trace(array[0]) //traces 2 - which is sorting ok

if your array looks like this
yes thank you I understand how this "should" be working.  however for some reason my trace output still shows as:

Jeremyville 30
Billie Joe Armstrong 10
Megan Whitmars 14
Noemi Riebesell 32
Hiroshi Fujiwara 75
Undefeated 70
Ryan Lombardi 16
Steve Monti 58
Mister Never 43

This is obviously not sorting by ID.  Even if I attempt to sort by "name" it will still not sort. I changed my sortOn to like your example.
//sort array
multiArr.sortOn("id");

Open in new window

Could the problem have something to do with how I am building the array?
ASKER CERTIFIED SOLUTION
Avatar of digitaldavenyc
digitaldavenyc

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
I did point out that you were sorting an array of numeric values and then went on to gave working examples of both ways.

You still accepted your own solution.

(off-topic comments removed)