Skip to content

Instantly share code, notes, and snippets.

@postpostscript
Created March 7, 2016 17:41
Show Gist options
  • Save postpostscript/2ded48865c544f1624dd to your computer and use it in GitHub Desktop.
Save postpostscript/2ded48865c544f1624dd to your computer and use it in GitHub Desktop.
function sort(rows, key, dir) {
if (!key || !rows || typeof rows.sort !== "function" || !rows.length) {
return rows;
}
var dir = dir ? String(dir).toLowerCase() : "up";
if (typeof key === "function") {
return rows.sort(key);
} else {
return rows.sort(function(rowA, rowB) {
if (dir === "u" || dir === "up" || dir === "asc") {
return rowA[key] > rowB[key];
} else {
return rowA[key] < rowB[key];
}
});
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment