Learn, grow and help others with BBBootstrap
Contribute us with some awesome cool snippets using HTML,CSS,JAVASCRIPT and BOOTSTRAP
![](https://i.imgur.com/dnN38NC.jpg)
Sorting objects in javascript ES6. we can use the id of the Objects of Array in javascript es6.
const alphanumbers = [ {id:8,name:'john'}, {id:4,name:'Kendey'}, {id:1,name:'Marry'}, {id:10,name:'Rodeo'}, {id:6,name:'Drive'} ]; const sortnumbers = alphanumbers.sort((a,b) => { return a.id - b.id; }); console.log(sortnumbers);
To sort the items on the basis of id try the following code
const main = [ {id: 1, name: 'The items of the first'}, {id: 2, name: 'The items of the second'}, {id: 3, name: 'The items of the third'}, {id: 4, name: 'The items of the Fourth'} ] const items = main.sort((a,b) => (a.id > b.id) ? 1 : ((b.id > a.id) ? -1 : 0)); console.log(items);
and if you wants to sort the items on the basis of name try the following code.
const main = [ {id: 1, name: 'The items of the first'}, {id: 2, name: 'The items of the second'}, {id: 3, name: 'The items of the third'}, {id: 4, name: 'The items of the Fourth'} ] const items = main.sort((a,b) => (a.name > b.name) ? 1 : ((b.name > a.name) ? -1 : 0)); console.log(items);