We help IT Professionals succeed at work.

problem sorting an Array via "sortOn" ...

digitaldavenyc
on
Medium Priority
896 Views
Last Modified: 2013-11-11
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

Comment
Watch Question

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

Author

Commented:
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

Author

Commented:
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

Author

Commented:
Could the problem have something to do with how I am building the array?
The problem was with how i was building the array ... someone on actionscript.org saw the problem.


//instead of building the array this way ....
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;
 
//create it much simpler this way
multiArr.push( { id:parseInt(data[i].artist[j].id), name:data[i].artist[j].name } );

Open in new window

Not the solution you were looking for? Getting a personalized solution is easy.

Ask the Experts
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)
Access more of Experts Exchange with a free account
Thanks for using Experts Exchange.

Create a free account to continue.

Limited access with a free account allows you to:

  • View three pieces of content (articles, solutions, posts, and videos)
  • Ask the experts questions (counted toward content limit)
  • Customize your dashboard and profile

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.