Skip to content

Instantly share code, notes, and snippets.

@KamalHunzai
Forked from jherax/sortBy.js
Created July 13, 2016 05:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save KamalHunzai/22a41d4e793aa31773ffc44e6c76b26c to your computer and use it in GitHub Desktop.
Save KamalHunzai/22a41d4e793aa31773ffc44e6c76b26c to your computer and use it in GitHub Desktop.
Sorting Arrays (Schwartzian transform)
/**
* Learn about Schwartzian transform
* https://en.wikipedia.org/wiki/Schwartzian_transform
*/
(function() {
if (typeof Object.defineProperty === 'function') {
try {
Object.defineProperty(Array.prototype, 'sortBy', { value: sortBy });
} catch (e) {}
}
if (!Array.prototype.sortBy) {
Array.prototype.sortBy = sortBy;
}
var _DESC = (/^desc\:\s*/i);
//comparison criteria
function _comparer(prev, next) {
var asc = 1;
if (typeof prev === "string") {
if (_DESC.test(prev)) asc = -1;
}
if (prev === next) return 0;
return (prev > next ? 1 : -1) * asc;
}
function sortBy(parser) {
var i, item,
arrLength = this.length;
if (typeof parser !== "function") {
return this.sort(Array.prototype.sort.bind(this));
}
//Schwartzian transform (decorate-sort-undecorate)
for (i = arrLength; i;) {
item = this[i -= 1];
//decorate the array
this[i] = [].concat(parser.call(null, item, i), item);
//console.log('decorated: ', this[i]);
}
//sort the array
this.sort(function(prev, next) {
var sorted, length = prev.length;
for (i = 0; i < length; i += 1) {
sorted = _comparer(prev[i], next[i]);
if (sorted) return sorted;
}
return 0;
});
//undecorate the array
for (i = arrLength; i;) {
item = this[i -= 1];
this[i] = item[item.length - 1];
}
return this;
}
}());
//writes the object as string
function write(obj) {
return JSON.stringify(obj, null, 2);
}
var arr = [494316000000, 415778400000, 346485600000, 494316000000];
console.warn(JSON.stringify(arr));
console.info("DEFAULT SORT (ASC)");
console.log(write(arr.sortBy()));
arr = ["1990/05/12", "1983/03/06", "1980/12/24", "1985/08/31"];
console.warn(JSON.stringify(arr));
console.info("SORT ASCENDING AS Date");
arr.sortBy(function(o) { return new Date(o) });
console.log(write(arr));
console.info("SORT DESCENDING AS String");
arr.sortBy(function(o) { return "DESC:" + o });
console.log(write(arr));
arr = [{ n: 8, d: "1985/08/31" }, { n: 2, d: "1980/12/24" }, { n: 5, d: "1983/03/06" }, { n: 8, d: "1983/03/06" }];
console.warn(JSON.stringify(arr));
console.info("SORT BY @n DESC, AFTER BY @d ASC AS Date");
arr.sortBy(function(o) { return [-o.n, new Date(o.d)] });
console.log(write(arr));
console.info("SORT BY @n ASC, AFTER BY @d DESC AS Date");
arr.sortBy(function(o) { return [o.n, -new Date(o.d)] });
console.log(write(arr));
arr = [{ id: 4, name: "pedro" }, { id: 6, name: "Lucia" }, { id: 7, name: "Paco" }, { id: 3, name: "luis" }];
console.warn(JSON.stringify(arr));
console.info("SORT BY @name ASC (ignore case)");
//toUpperCase() used to ignore case sensitive
arr.sortBy(function(o) { return o.name.toUpperCase() });
console.log(write(arr));
console.info("SORT BY @name DESC (ignore case)");
//toUpperCase() used to ignore case sensitive
arr.sortBy(function(o) { return 'DESC:' + o.name.toUpperCase() });
console.log(write(arr));
arr = [{ id: 4, name: "pedro", age: 26 }, { id: 6, name: "Pedro", age: 32 }, { id: 7, name: "Luis", age: 26 }, { id: 2, name: "luis", age: 26 }];
console.warn(JSON.stringify(arr));
console.info("SORT BY @name ASC (ignore case), AFTER BY @age DESC, AFTER BY @id ASC");
//toUpperCase() used to ignore case sensitive
arr.sortBy(function(o) { return [o.name.toUpperCase(), -o.age, o.id] });
console.log(write(arr));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment