Skip to content

Instantly share code, notes, and snippets.

@christoph-daehne
Created May 30, 2016 15:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save christoph-daehne/d94584677619d5465fadc24d2af3dbf3 to your computer and use it in GitHub Desktop.
Save christoph-daehne/d94584677619d5465fadc24d2af3dbf3 to your computer and use it in GitHub Desktop.
import Ember from 'ember';
/**
* use "{{yield sorted}}" as handlebars template
*/
export default Ember.Component.extend({
list: undefined,
property: undefined,
sorted: function () {
const list = this.get("list");
if (!list) {
return [];
}
return list.sort((a, b) => {
const x = this._getValue(a);
const y = this._getValue(b);
return (x < y) ? -1 : (x > y) ? 1 : 0;
});
}.property("list", "property"),
_getValue(x) {
const property = this.get("property");
const value = property === null ? x : x[property];
if ((typeof value) === "string") {
return value.toLocaleLowerCase();
} else {
return value;
}
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment