Techie Zone

|

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.

0 comments:

Post a Comment

Note: Only a member of this blog may post a comment.

 

©2009 Programming & Security | Template Blue by TNB

Vida de bombeiro Recipes Informatica Humor Jokes Mensagens Curiosity Saude Video Games Diario das Mensagens Eletronica Rei Jesus News Noticias da TV Artesanato Esportes Noticias Atuais Games Pets Career Religion Recreation Business Education Autos Academics Style Television Programming Motosport Humor News The Games Home Downs World News Internet Design Entertaimment Celebrities 1001 Games Doctor Pets Net Downs World Enter Jesus Mensagensr Android Rub Letras Dialogue cosmetics Genexus Só Humor Curiosity Gifs Medical Female American Health Madeira Designer PPS Divertidas Estate Travel Estate Writing Computer Matilde Ocultos futebolcomnoticias girassol lettheworldturn topdigitalnet Bem amado enjohnny produceideas foodasticos cronicasdoimaginario downloadsdegraca compactandoletras newcuriosidades blogdoarmario arrozinhoii sonasol halfbakedtaters make-it-plain amatha lisboaohyeah lasofia thebushrajr wingshock tripedes gorduravegetal dainfamia dejavu-transpersonal jsbenfica republicadasbadanas ruiherbon iranianforaryans eaystcheyl fotosdanadir ojosmasabiertos ceilasantos