Techie Zone |
Sort Javascript Object by Value Posted: 30 Aug 2014 02:19 AM PDT I needed to sort data in an JavaScript object, as per JavaScript API there is no direct way to sort JavaScript objects. In this article we will learn how we can sort the javascript objects based on some values. To start with we would create an set of multiple objects having countryname and population in that country. //Object 1 var countryData1 = {}; countryData1.country = 'India'; countryData1.population = 123456; //Object2 var countryData2 = {}; countryData2.country = 'China'; countryData2.population = 12345622; //Object3 var countryData3 = {}; countryData3.country = 'Australia'; countryData3.population = 1234;
Now we would store all the data inside an array. var customerData = []; customerData.push(countryData1); customerData.push(countryData2); customerData.push(countryData3); Now we would sort the country data based on population. customerData.sort(function(a, b){return b.population - a.population}); That’s it now we have customerData array sorted based on country population in descending order. The post Sort Javascript Object by Value appeared first on Techie Zone. |
You are subscribed to email updates from Techie Zone To stop receiving these emails, you may unsubscribe now. | Email delivery powered by Google |
Google Inc., 20 West Kinzie, Chicago IL USA 60610 |
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.