HighDots Forums  

Re: Sorting JSON data

Javascript JavaScript language (comp.lang.javascript)


Discuss Re: Sorting JSON data in the Javascript forum.



Reply
 
Thread Tools Display Modes
  #1  
Old   
Martin Honnen
 
Posts: n/a

Default Re: Sorting JSON data - 01-09-2009 , 10:28 AM






laredotornado wrote:

Quote:
For example, if my array were

var featureData = [{name:'BCD',description:'aaa'},
{name:'aaa',description:'bbb'},{name:'ccc',descrip tion:'wewfwef'}];

How could I sort the array based off the elements in the "name"
attribute?
Your array has a method named sort to which you need to pass a comparer
function:

var featureData = [{name:'BCD',description:'aaa'},
{name:'aaa',description:'bbb'},{name:'ccc',descrip tion:'wewfwef'}];

featureData.sort(function (obj1, obj2) {
return obj1.name < obj2.name ? -1 :
(obj1.name > obj2.name ? 1 : 0);
});


See
https://developer.mozilla.org/en/Cor...cts/Array/sort

--

Martin Honnen
http://JavaScript.FAQTs.com/


Reply With Quote
Reply




Thread Tools
Display Modes

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

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



Powered by vBulletin Version 3.5.4
Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.