From 279edce92eff17b0234cf8335898b57e84f9b293 Mon Sep 17 00:00:00 2001 From: Brock Whittaker Date: Thu, 28 Sep 2017 14:50:33 -0700 Subject: [PATCH] list_render: Add generic sort functions. This adds generic sort functions such as sorting by alphabetic order and by numeric order (which involves parsing the numbers first). --- static/js/list_render.js | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/static/js/list_render.js b/static/js/list_render.js index c9881b7db1..09f229e6f0 100644 --- a/static/js/list_render.js +++ b/static/js/list_render.js @@ -274,6 +274,31 @@ var list_render = (function () { prototype.__set_events(); + // add built-in generic sort functions. + prototype.add_generic_sort_function("alphabetic", function (prop) { + return function (a, b) { + if (a[prop] > b[prop]) { + return 1; + } else if (a[prop] === b[prop]) { + return 0; + } + + return -1; + }; + }); + + prototype.add_generic_sort_function("numeric", function (prop) { + return function (a, b) { + if (parseFloat(a[prop]) > parseFloat(b[prop])) { + return 1; + } else if (parseFloat(a[prop]) === parseFloat(b[prop])) { + return 0; + } + + return -1; + }; + }); + // Save the instance for potential future retrieval if a name is provided. if (opts.name) { DEFAULTS.instances[opts.name] = prototype;