javascript - Sort by name and number -


i have array

people = [         { "name": "bob mike", "nickname": "john" , "points": 5 ) },         { "name": "andrea maria", "nickname": "bob", "points": 5 )}         { "name": "larry kiu", "nickname": "larry", "points": 4 ) }     ]; 

i want sort this

andrea maria - 5 bob mike - 5 larry kiu - 4 

i'm not sort method, found little code, sorts points, not including asc name field.

people.sort(dynamicsort('name')).sort(dynamicsort('points'));  function dynamicsort(property) {return function(a, b) {    return (a[property] > b[property]) ? -1 : (a[property] < b[property])? 1 : 0; }} 

edit: thank erazihel

you can use localecompare compare 2 strings

const people = [   { name: "bob mike", nickname: "john" , points: 5 },   { name: "andrea maria", nickname: "bob", points: 5 },   { name: "larry kiu", nickname: "larry", points: 4 }  ];    people.sort(function(a, b) {    return b.points - a.points || a.name.localecompare(b.name)  });    console.log(people.map(a => a.name + ' - ' + a.points));


Comments

Popular posts from this blog

Is there a better way to structure post methods in Class Based Views -

performance - Why is XCHG reg, reg a 3 micro-op instruction on modern Intel architectures? -

jquery - Responsive Navbar with Sub Navbar -