Sort objects in an array alphabetically on one property of the array
you would have to do something like this:
objArray.sort(function(a, b) {
var textA = a.DepartmentName.toUpperCase();
var textB = b.DepartmentName.toUpperCase();
return (textA < textB) ? -1 : (textA > textB) ? 1 : 0;
});
note: changing the case (to upper or lower) ensures a case insensitive sort.
Sort objects in an array alphabetically based on one property
Using the comment by @gurvinder372 as influence, you should do the following:
myArray.sort(function (a, b) {
var textA = a.name.toUpperCase();
var textB = b.name.toUpperCase();
return textA.localeCompare(textB);
});
Sort array of objects by string property value
It's easy enough to write your own comparison function:
function compare( a, b ) {
if ( a.last_nom < b.last_nom ){
return -1;
}
if ( a.last_nom > b.last_nom ){
return 1;
}
return 0;
}
objs.sort( compare );
Or inline (c/o Marco Demaio):
objs.sort((a,b) => (a.last_nom > b.last_nom) ? 1 : ((b.last_nom > a.last_nom) ? -1 : 0))
Or simplified for numeric (c/o Andre Figueiredo):
objs.sort((a,b) => a.last_nom - b.last_nom); // b - a for reverse sort
Sorting an array of objects alphabetically
cars = [{
id: 1,
items: [{
name: 'ab',
description: 'this is car1 description'
},{
name: 'cd',
description: 'this is car2 description'
},{
name: 'car3',
description: 'this is car3 description'
},{
name: 'aaa',
description: 'this is car4 description'
},{
name: 'car5',
description: 'this is car5 description'
}]
}];
cars[0].items.sort((a,b) => a.name > b.name ? 1 : -1)
Sort array by firstname (alphabetically) in JavaScript
Suppose you have an array users
. You may use users.sort
and pass a function that takes two arguments and compare them (comparator)
It should return
- something negative if first argument is less than second (should be placed before the second in resulting array)
- something positive if first argument is greater (should be placed after second one)
- 0 if those two elements are equal.
In our case if two elements are a
and b
we want to compare a.firstname
and b.firstname
Example:
users.sort(function(a, b){
if(a.firstname < b.firstname) { return -1; }
if(a.firstname > b.firstname) { return 1; }
return 0;
})
This code is going to work with any type.
Note that in "real life"™ you often want to ignore case, correctly sort diacritics, weird symbols like ß, etc. when you compare strings, so you may want to use localeCompare
. See other answers for clarity.
Sorting an array of objects by property values
Sort homes by price in ascending order:
homes.sort(function(a, b) {
return parseFloat(a.price) - parseFloat(b.price);
});
Or after ES6 version:
homes.sort((a, b) => parseFloat(a.price) - parseFloat(b.price));
Some documentation can be found here.
For descending order, you may use
homes.sort((a, b) => parseFloat(b.price) - parseFloat(a.price));
Sort JavaScript array of Objects based on one of the object's properties
There are 2 basic ways:
var arr = [{name:"ABC"},{name:"BAC"},{name:"abc"},{name:"bac"}];
arr.sort(function(a,b){
var alc = a.name.toLowerCase(), blc = b.name.toLowerCase();
return alc > blc ? 1 : alc < blc ? -1 : 0;
});
or
arr.sort(function(a,b){
return a.name.toLowerCase().localeCompare(b.name.toLowerCase());
});
Be aware that the 2nd version ignore diacritics, so a
and à
will be sorted as the same letter.
Now the problem with both these ways is that they will not sort uppercase ABC
before lowercase abc
, since it will treat them as the same.
To fix that, you will have to do it like this:
arr.sort(function(a,b){
var alc = a.name.toLowerCase(), blc = b.name.toLowerCase();
return alc > blc ? 1 : alc < blc ? -1 : a.name > b.name ? 1 : a.name < b.name ? -1 : 0;
});
Again here you could choose to use localeCompare
instead if you don't want diacritics to affect the sorting like this:
arr.sort(function(a,b){
var lccomp = a.name.toLowerCase().localeCompare(b.name.toLowerCase());
return lccomp ? lccomp : a.name > b.name ? 1 : a.name < b.name ? -1 : 0;
});
You can read more about sort here: https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Array/sort
Sort an array of object by a property (with custom order, not alphabetically)
You could take an object for the wanted order.
var array = [{ code: "RED", value: 0 }, { code: "BLUE", value: 0 }, { code: "RED", value: 0 }, { code: "GREEN", value: 0 }, { code: "BLUE", value: 0 }, { code: "RED", value: 0 }, { code: "GREEN", value: 0 }, { code: "BLUE", value: 0 }],
order = { GREEN: 1, BLUE: 2, RED: 3 };
array.sort(function (a, b) {
return order[a.code] - order[b.code];
});
console.log(array);
.as-console-wrapper { max-height: 100% !important; top: 0; }
How to sort array of objects alphabetically except for numbers?
You was half way, String#localeCompare
accepts an option where you can use a natural sorting with strings.
const
array = [{ name: 'product-2' }, { name: 'product-15' }, { name: 'product-3' }, { name: 'product-10' }];
array.sort((a, b) => a.name.localeCompare(
b.name,
undefined,
{ numeric: true, sensitivity: 'base' }
));
console.log(array);
Related Topics
Incorrect Result in JavaScript Calculation
Why Can't I Update Props in React.Js
Javascript: Get Argument Value and Name of Passed Variable
How to Highlight the Text of the Dom Range Object
JavaScript Change Date into Format of (Dd/Mm/Yyyy)
Best Practice for Using Window.Onload
Trigger Right Click Using Pure JavaScript
JavaScript Decoding HTML Entities
Canvas Image Crossplatform Insecure Error
Using Ajax to Read Local Files
Jquery .On() Method with Multiple Event Handlers to One Selector
Es6 Exporting/Importing in Index File
Array.Push() Makes All Elements the Same When Pushing an Object