[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 819
  • Last Modified:

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

0
digitaldavenyc
Asked:
digitaldavenyc
  • 4
  • 3
1 Solution
 
scooby_56Commented:
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

0
 
digitaldavenycAuthor 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".
0
 
scooby_56Commented:
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
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
digitaldavenycAuthor 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

0
 
digitaldavenycAuthor Commented:
Could the problem have something to do with how I am building the array?
0
 
digitaldavenycAuthor Commented:
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

0
 
scooby_56Commented:
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)
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

  • 4
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now