Created
March 7, 2016 17:41
-
-
Save postpostscript/2ded48865c544f1624dd to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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