Skip to content

Instantly share code, notes, and snippets.

@staghouse
Created February 13, 2019 11:08
Show Gist options
  • Save staghouse/3af947d9c519adc31995160febc558f0 to your computer and use it in GitHub Desktop.
Save staghouse/3af947d9c519adc31995160febc558f0 to your computer and use it in GitHub Desktop.
Expects a string with a ':' delimiter. Meant for Vue.js select markup elements and table rendering
sortBy(event = '') {
let values = event.currentTarget
? event.currentTarget.value.split(':')
: event.split(':');
let cfg = {
prop: values[0],
desc: values[1] ? -1 : 1,
parser: function(x) {
return x;
},
};
let parse = function(x) {
return x;
};
let getItem = function(x) {
var isObject = x != null && typeof x === 'object';
var isProp = isObject && this.prop in x;
return this.parser(isProp ? x[this.prop] : x);
};
return this.defaultList.sort((a, b) => {
a = getItem.call(cfg, a);
b = getItem.call(cfg, b);
return cfg.desc * (a < b ? -1 : +(a > b));
});
},
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment