const books =[{id:1,name:'The Lord of the Rings'},{id:2,name:'A Tale of Two Cities'},{id:3,name:'Don Quixote'},{id:4,name:'The Hobbit'}]
books.sort((a,b)=>(a.name> b.name)?1:((b.name> a.name)?-1:0));
// Price Low To High
array?.sort((a, b)=>(a.price> b.price?1:-1))// Price High To Low
array?.sort((a, b)=>(a.price> b.price?-1:1))// Name A to Z
array?.sort((a, b)=>(a.name> b.name?1:1))// Name Z to A
array?.sort((a, b)=>(a.name> b.name?-1:1))// Sort by date
array.sort((a,b)=>newDate(b.date)-newDate(a.date));
const subjects =[{"name":"Math","score":81},{"name":"English","score":77},{"name":"Chemistry","score":87},{"name":"Physics","score":84}];// Sort in ascending order - by name
subjects.sort((a, b)=>(a.name> b.name)?1:-1);console.log(subjects);
const books =[{id:1,name:'The Lord of the Rings'},{id:2,name:'A Tale of Two Cities'},{id:3,name:'Don Quixote'},{id:4,name:'The Hobbit'}]
books.sort((a, b)=> a.name.localeCompare(b.name))
var data =[{h_id:"3",city:"Dallas",state:"TX",zip:"75201",price:"162500"},{h_id:"4",city:"Bevery Hills",state:"CA",zip:"90210",price:"319250"},{h_id:"6",city:"Dallas",state:"TX",zip:"75000",price:"556699"},{h_id:"5",city:"New York",state:"NY",zip:"00010",price:"962500"}];
data.sort(function(a, b){return a.city.localeCompare(b.city)|| b.price- a.price;});console.log(data);
functionsortByDate(a, b){if( a.created_at< b.created_at){return-1;}if( a.created_at> b.created_at){return1;}return0;}
myDates.sort(sortByDate);//myDates is not sorted.
functioncompareFn(a, b){if(a is less than b by some ordering criterion){return-1;}if(a is greater than b by the ordering criterion){return1;}// a must be equal to breturn0;}
let persolize=[{key:'speakers',view:10},{key:'test',view:5}]
persolize.sort((a,b)=> a.view- b.view);//If it only array and not an array object, use below statement//persolize.sort((a,b) => a - b);