Let's say you have a JavaScript class like this
var DepartmentFactory = function(data) {
this.id = data.Id;
this.name = data.DepartmentName;
this.active = data.Active;
}
Let's say you then create a number of instances of that class and store them in an array
var objArray = [];
objArray.push(DepartmentFactory({Id: 1, DepartmentName: 'Marketing', Active: true}));
objArray.push(DepartmentFactory({Id: 2, DepartmentName: 'Sales', Active: true}));
objArray.push(DepartmentFactory({Id: 3, DepartmentName: 'Development', Active: true}));
objArray.push(DepartmentFactory({Id: 4, DepartmentName: 'Accounting', Active: true}));
So I now would have an array of objects created by DepartmentFactory
. How would I go about using the array.sort()
method to sort this array of objects by the DepartmentName
property of each object?
The array.sort()
method works just fine when sorting an array of strings
var myarray=["Bob", "Bully", "Amy"];
myarray.sort(); //Array now becomes ["Amy", "Bob", "Bully"]
But how do I make it work with a list of objects?
This question is related to
javascript
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.
To support unicode:
objArray.sort(function(a, b) {
return a.DepartmentName.localeCompare(b.DepartmentName);
});
var DepartmentFactory = function(data) {
this.id = data.Id;
this.name = data.DepartmentName;
this.active = data.Active;
}
// use `new DepartmentFactory` as given below. `new` is imporatant
var objArray = [];
objArray.push(new DepartmentFactory({Id: 1, DepartmentName: 'Marketing', Active: true}));
objArray.push(new DepartmentFactory({Id: 2, DepartmentName: 'Sales', Active: true}));
objArray.push(new DepartmentFactory({Id: 3, DepartmentName: 'Development', Active: true}));
objArray.push(new DepartmentFactory({Id: 4, DepartmentName: 'Accounting', Active: true}));
function sortOn(property){
return function(a, b){
if(a[property] < b[property]){
return -1;
}else if(a[property] > b[property]){
return 1;
}else{
return 0;
}
}
}
//objArray.sort(sortOn("id")); // because `this.id = data.Id;`
objArray.sort(sortOn("name")); // because `this.name = data.DepartmentName;`
console.log(objArray);
// Sorts an array of objects "in place". (Meaning that the original array will be modified and nothing gets returned.)
function sortOn (arr, prop) {
arr.sort (
function (a, b) {
if (a[prop] < b[prop]){
return -1;
} else if (a[prop] > b[prop]){
return 1;
} else {
return 0;
}
}
);
}
//Usage example:
var cars = [
{make:"AMC", model:"Pacer", year:1978},
{make:"Koenigsegg", model:"CCGT", year:2011},
{make:"Pagani", model:"Zonda", year:2006},
];
// ------- make -------
sortOn(cars, "make");
console.log(cars);
/* OUTPUT:
AMC : Pacer : 1978
Koenigsegg : CCGT : 2011
Pagani : Zonda : 2006
*/
// ------- model -------
sortOn(cars, "model");
console.log(cars);
/* OUTPUT:
Koenigsegg : CCGT : 2011
AMC : Pacer : 1978
Pagani : Zonda : 2006
*/
// ------- year -------
sortOn(cars, "year");
console.log(cars);
/* OUTPUT:
AMC : Pacer : 1978
Pagani : Zonda : 2006
Koenigsegg : CCGT : 2011
*/
objArray.sort((a, b) => a.DepartmentName.localeCompare(b.DepartmentName))
Shorter code with ES6
objArray.sort((a, b) => a.DepartmentName.toLowerCase().localeCompare(b.DepartmentName.toLowerCase()))
objArray.sort( (a, b) => a.id.localeCompare(b.id, 'en', {'sensitivity': 'base'}));
This sorts them alphabetically AND is case insensitive. It's also super clean and easy to read :D
var DepartmentFactory = function(data) {
this.id = data.Id;
this.name = data.DepartmentName;
this.active = data.Active;
}
var objArray = [];
objArray.push(new DepartmentFactory({Id: 1, DepartmentName: 'Marketing', Active: true}));
objArray.push(new DepartmentFactory({Id: 2, DepartmentName: 'Sales', Active: true}));
objArray.push(new DepartmentFactory({Id: 3, DepartmentName: 'Development', Active: true}));
objArray.push(new DepartmentFactory({Id: 4, DepartmentName: 'Accounting', Active: true}));
console.log(objArray.sort(function(a, b) { return a.name > b.name}));
do it like this
objArrayy.sort(function(a, b){
var nameA=a.name.toLowerCase(), nameB=b.name.toLowerCase()
if (nameA < nameB) //sort string ascending
return -1
if (nameA > nameB)
return 1
return 0 //default return value (no sorting)
});
console.log(objArray)
Here is a simple function you can use to sort array of objects through their properties; it doesn't matter if the property is a type of string or integer, it will work.
var cars = [_x000D_
{make:"AMC", model:"Pacer", year:1978},_x000D_
{make:"Koenigsegg", model:"CCGT", year:2011},_x000D_
{make:"Pagani", model:"Zonda", year:2006},_x000D_
];_x000D_
_x000D_
function sortObjectsByProp(objectsArr, prop, ascending = true) {_x000D_
let objectsHaveProp = objectsArr.every(object => object.hasOwnProperty(prop));_x000D_
if(objectsHaveProp) {_x000D_
let newObjectsArr = objectsArr.slice();_x000D_
newObjectsArr.sort((a, b) => {_x000D_
if(isNaN(Number(a[prop]))) {_x000D_
let textA = a[prop].toUpperCase(),_x000D_
textB = b[prop].toUpperCase();_x000D_
if(ascending) {_x000D_
return textA < textB ? -1 : textA > textB ? 1 : 0;_x000D_
} else {_x000D_
return textB < textA ? -1 : textB > textA ? 1 : 0;_x000D_
}_x000D_
} else {_x000D_
return ascending ? a[prop] - b[prop] : b[prop] - a[prop];_x000D_
}_x000D_
});_x000D_
return newObjectsArr;_x000D_
}_x000D_
return objectsArr;_x000D_
}_x000D_
_x000D_
let sortedByMake = sortObjectsByProp(cars, "make"); // returns ascending order by its make;_x000D_
let sortedByYear = sortObjectsByProp(cars, "year", false); // returns descending order by its year,since we put false as a third argument;_x000D_
console.log(sortedByMake);_x000D_
console.log(sortedByYear);
_x000D_
You have to pass a function that accepts two parameters, compares them, and returns a number, so assuming you wanted to sort them by ID you would write...
objArray.sort(function(a,b) {
return a.id-b.id;
});
// objArray is now sorted by Id
After try a little bit on this, and trying to make less loops as possible, I ended up with this solution:
const items = [
{
name: 'One'
},
{
name: 'Maria is here'
},
{
name: 'Another'
},
{
name: 'Z with a z'
},
{
name: '1 number'
},
{
name: 'Two not a number'
},
{
name: 'Third'
},
{
name: 'Giant'
}
];
const sorted = items.sort((a, b) => {
return a[name] > b[name];
});
let sortedAlphabetically = {};
for(var item in sorted) {
const firstLetter = sorted[item].name[0];
if(sortedAlphabetically[firstLetter]) {
sortedAlphabetically[firstLetter].push(sorted[item]);
} else {
sortedAlphabetically[firstLetter] = [sorted[item]];
}
}
console.log('sorted', sortedAlphabetically);
A simple answer:
objArray.sort(function(obj1, obj2) {
return obj1.DepartmentName > obj2.DepartmentName;
});
ES6 way:
objArray.sort((obj1, obj2) => {return obj1.DepartmentName > obj2.DepartmentName};
If you need to make it lowercase/uppercase etc, just do that and store that result in a variable than compare that variable. Example:
objArray.sort((obj1, obj2) => {
var firstObj = obj1.toLowerCase();
var secondObj = obj2.toLowerCase();
return firstObj.DepartmentName > secondObj.DepartmentName;
});
Source: Stackoverflow.com