Sorting an array of objects by a property

I have an array that looks like this:

var people = [
{name: "John", age:15},
{name: "Betty", age:12},
{name: "Zach", age:22}
];

 

And I need to sort it by name, here's how to do it:

people = people.sort(function(a, b){

var aName = a.name.toLowerCase();
var bName = b.name.toLowerCase();

if(aName > bName) return 1;

if(aName < bName) return -1;

return 0;

});

 

Add a Comment...

 

Comments

Hello this is kinda of off topic but I was wanting to know if blogs use WYSIWYG editors or if you have to manually code with HTML. I'm starting a blog soon but have no coding expertise so I wanted to get guidance from someone with experience. Any help would be greatly appreciated! liverpool trøjer - 5/15/2017

HollisGoc Bayern Munchen Ernestine MarilynCa Juventuss Blakezcow DorrisWhi Juventus KarolynLo LillieHam Barcelona YongDonov ZVVLyndaw Leicester City JenniStel DomingoHe Italien JoannaNel EltonMils Nederlanderna RonaldSim KathleneW Mexico MeghanMes Wilburuqb Colombia UrsulaApp Velvastvn Inter Milan TroyBourn - 3/07/2017

I find the top 5 luxury wedding dresses in 2015. Medical profession has seen many quacks. Viagra works only for 4-6 hours. WeвÐâ¢ll find the best paper writing service for you! How to choose the best custom essay writing services Getting a Better Education: College Tips and Tricks Win the War for Self-Confidence вÐâ Here are Your 10 Weapons TrustMyPaper.com reviews, prices and discounts This easy to use ice breaker works great with teleconferences too and allows team memb - 2/20/2017

Drink a glass of water with 2 teaspoons of vinegar 3 times a day. Wall Street Journal is going too far this time. Buy Research Paper | Purchase Custom Research Paper - No Plagiarism Master's Thesis | Custom Master Thesis Writing Service Thesis Proposal | Custom Thesis Proposal Writing online Assignment Writing | Custom Writing Assignments Online - $10/page Buy Dissertation | Purchase Custom Written Dissertations Online The effect is just amazing! Viagra can help you have an erec - 2/12/2017