Skip to content

Instantly share code, notes, and snippets.

@bkawk
Last active July 5, 2016 06:30
Show Gist options
  • Save bkawk/fcd907955e0e981ca8380d7d2210488a to your computer and use it in GitHub Desktop.
Save bkawk/fcd907955e0e981ca8380d7d2210488a to your computer and use it in GitHub Desktop.
Sort objects in array
// SORTS ELEMENTS OF AN ARRAY
// http://jsbin.com/rutumeh/edit?js,console
var sortBy = (function () {
var _toString = Object.prototype.toString,
//the default parser function
_parser = function (x) { return x; },
//gets the item to be sorted
_getItem = function (x) {
return this.parser((x !== null && typeof x === "object" && x[this.prop]) || x);
};
/* PROTOTYPE VERSION */
// Creates the sort method in the Array prototype
Object.defineProperty(Array.prototype, "sortBy", {
configurable: false,
enumerable: false,
// @o.prop: property name (if it is an Array of objects)
// @o.desc: determines whether the sort is descending
// @o.parser: function to parse the items to expected type
value: function (o) {
if (_toString.call(o) !== "[object Object]")
o = {};
if (typeof o.parser !== "function")
o.parser = _parser;
o.desc = !!o.desc ? -1 : 1;
return this.sort(function (a, b) {
a = _getItem.call(o, a);
b = _getItem.call(o, b);
return o.desc * (a < b ? -1 : +(a > b));
//return ((a > b) - (b > a)) * o.desc;
});
}
});
/* FUNCTION VERSION */
// Sorts the elements of an array
// @array: the Array
// @o.prop: property name (if it is an Array of objects)
// @o.desc: determines whether the sort is descending
// @o.parser: function to parse the items to expected type
return function (array, o) {
if (!(array instanceof Array) || !array.length)
return [];
if (_toString.call(o) !== "[object Object]")
o = {};
if (typeof o.parser !== "function")
o.parser = _parser;
o.desc = !!o.desc ? -1 : 1;
return array.sort(function (a, b) {
a = _getItem.call(o, a);
b = _getItem.call(o, b);
return o.desc * (a < b ? -1 : +(a > b));
});
};
}());
//--------------------------------------------------------
var aNumbers = [10,8,5,3,7,4,5,1];
var aObject = [
{name: "david", age: 30},
{name: "Luis", age: 24},
{name: "julian", age: 24},
{name: "alex", age: 36},
{name: "Samuel", age: 28},
{name: "Diana", age: 25}
];
console.warn("> NUMERIC ARRAY");
console.log(aNumbers);
// PROTOTYPE VERSION
console.info("ASCENDING ORDER");
console.log(aNumbers.sortBy());
// FUNCTION VERSION
console.info("DESCENDING ORDER");
sortBy(aNumbers, { desc: true });
console.log(aNumbers);
console.warn("> ARRAY OF OBJECTS");
console.log(aObject);
// PROTOTYPE VERSION
console.info("ASCENDING BY @age");
aObject.sortBy({ prop: "age" });
console.log([].concat(aObject));
// FUNCTION VERSION
console.info("DESCENDING BY @name (ignore case)");
sortBy(aObject, {
prop: "name",
desc: true,
parser: function (item) {
//ignore case sensitive
return item.toUpperCase();
}
});
console.log([].concat(aObject));
/* Sorts the array by Date
aObject.sortBy({
prop: "dob",
desc: true,
parser: function (item) { return new Date(item); }
});
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment