sorting array of objects in javascript by using select option
var items = [
{ name: 'Edward', value: 21 },
{ name: 'Sharpe', value: 37 },
{ name: 'And', value: 45 },
{ name: 'The', value: -12 },
{ name: 'Magnetic', value: 13 },
{ name: 'Zeros', value: 37 }
];
// sort by value
items.sort(function (a, b) {
return a.value - b.value;
});
// sort by name
items.sort(function(a, b) {
var nameA = a.name.toUpperCase(); // ignore upper and lowercase
var nameB = b.name.toUpperCase(); // ignore upper and lowercase
if (nameA < nameB) {
return -1;
}
if (nameA > nameB) {
return 1;
}
// names must be equal
return 0;
});
Also in JavaScript:
- select option trigger in js
- string to number js
- javascript constructor object
- convert json into map in java example
- jquery select specific radio button by value
- react-dates nextjs
- how to calculate each row with on change table <td><input> jquery
- js hangman with repeated characters
- how to get element by attribute value in javascript
- discord.js channel create
- how to import a json string from a file in typescript
- .env not working on react
- trigger send parameter
- use effect like component did mount
- javascript reduce method stack overflow
- get ckeditor textarea value in jquery
- array includes
- javascript remove from array by index
- react pdf generator
- loop trough javascript array associative
- get methods of class javascript
- javascript get intersection of two arrays
- javascript date timezone
- redux saga fetch api