Skip to content

Instantly share code, notes, and snippets.

@jherax jherax/sortBy-old.js
Last active Nov 8, 2018

Embed
What would you like to do?
Sorting Arrays
/*
* Important!
* This snippet is deprecated, a best implementation of sortBy can be found here:
* https://github.com/jherax/array-sort-by
*/
var sortBy = (function () {
var toString = Object.prototype.toString,
// default parser function
parse = function (x) { return x; },
// gets the item to be sorted
getItem = function (x) {
var isObject = x != null && typeof x === "object";
var isProp = isObject && this.prop in x;
return this.parser(isProp ? x[this.prop] : x);
};
/**
* Sorts an array of elements.
*
* @param {Array} array: the collection to sort
* @param {Object} cfg: the configuration options
* @property {String} cfg.prop: property name (if it is an Array of objects)
* @property {Boolean} cfg.desc: determines whether the sort is descending
* @property {Function} cfg.parser: function to parse the items to expected type
* @return {Array}
*/
return function sortby (array, cfg) {
if (!(array instanceof Array && array.length)) return [];
if (toString.call(cfg) !== "[object Object]") cfg = {};
if (typeof cfg.parser !== "function") cfg.parser = parse;
cfg.desc = !!cfg.desc ? -1 : 1;
return array.sort(function (a, b) {
a = getItem.call(cfg, a);
b = getItem.call(cfg, b);
return cfg.desc * (a < b ? -1 : +(a > b));
});
};
}());
//---------------------------
function write(text, array) {
console.info(`%c${text}`, "color:brown");
console.log([].concat(array));
}
var aNumbers = [10, 8, 5, 3, 7, 4, 5, 1];
var aObject = [
{ name: "david", age: 30, date: "2011-11-14T17:25:45Z", achievement: 5 },
{ name: "Luis", age: 24, date: "2011-11-14T16:30:43Z", achievement: 23 },
{ name: "julian", age: 24, date: "2011-11-31T17:29:52Z", achievement: 12 },
{ name: "alex", age: 36, date: "2011-11-14T16:58:03Z", achievement: 0 },
{ name: "Samuel", age: 28, date: "2011-11-01T16:17:54Z", achievement: 9 },
{ name: "Diana", age: 25, date: "2011-11-14T17:07:21Z", achievement: 28 }
];
//---------------------------
console.warn("ARRAY OF NUMBERS");
console.log(aNumbers);
sortBy(aNumbers);
write("ASCENDING ORDER", aNumbers);
// expected: [1, 3, 4, 5, 5, 7, 8, 10]
sortBy(aNumbers, { desc: true });
write("DESCENDING ORDER", aNumbers);
// expected: [10, 8, 7, 5, 5, 4, 3, 1]
//--------------------------
console.warn("ARRAY OF OBJECTS");
console.log(aObject);
sortBy(aObject, { prop: "age" });
write("ASCENDING BY @age", aObject);
// expected:
// [0] = { name: "Luis", age: 24, date: "2011-11-14T16:30:43Z", achievement: 23 }
// [5] = { name: "alex", age: 36, date: "2011-11-14T16:58:03Z", achievement: 0 }
sortBy(aObject, { prop: "achievement", desc: true });
write("DESCENDING BY @achievement", aObject);
// expected:
// [0] = { name: "Diana", age: 25, date: "2011-11-14T17:07:21Z", achievement: 28 }
// [5] = { name: "alex", age: 36, date: "2011-11-14T16:58:03Z", achievement: 0 }
sortBy(aObject, {
prop: "name",
// ignores case sensitive
parser: (item) => item.toUpperCase()
});
write("ASCENDING BY @name", aObject);
// expected:
// [0] = { name: "alex", age: 36, date: "2011-11-14T16:58:03Z", achievement: 0 }
// [5] = { name: "Samuel", age: 28, date: "2011-11-01T16:17:54Z", achievement: 9 }
sortBy(aObject, {
prop: "date",
desc: true,
parser: (item) => new Date(item)
});
write("DESCENDING BY @date", aObject);
// expected:
// [0] = { name: "julian", age: 24, date: "2011-11-31T17:29:52Z", achievement: 12 }
// [5] = { name: "Samuel", age: 28, date: "2011-11-01T16:17:54Z", achievement: 9 }
@jherax

This comment has been minimized.

Copy link
Owner Author

commented May 26, 2015

I recommend GitHub: Array sortBy - a best implementation of sortBy method which uses the Schwartzian transform.

https://github.com/jherax/array-sort-by

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.