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).
This commit is contained in:
Brock Whittaker 2017-09-28 14:50:33 -07:00
parent 3706e2c6ba
commit 279edce92e
1 changed files with 25 additions and 0 deletions

View File

@ -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;