Skip to content

Instantly share code, notes, and snippets.

@solomonhawk
Last active August 29, 2015 14:12
Show Gist options
  • Save solomonhawk/3c4af5d84f734ea60c2c to your computer and use it in GitHub Desktop.
Save solomonhawk/3c4af5d84f734ea60c2c to your computer and use it in GitHub Desktop.
Simple Object Sorting
{
"name": "Ashe's Pokedex",
"sort": "byName",
"order": "descending",
"pokemon": [
{
"id": 4,
"name": "Charmander",
"types": "fire",
"species": "lizard",
"abilities": ["Blaze", "Solar Power"],
"stats": {
"hp": 39,
"attack": 52,
"defense": 43,
"speed": 65
}
},
{
"id": 7,
"name": "Squirtle",
"types": "water",
"species": "turtle",
"abilities": ["Torrent", "Rain Dash"],
"stats": {
"hp": 44,
"attack": 48,
"defense": 65,
"speed": 43
}
},
{
"id": 1,
"name": "Bulbasaur",
"types": ["grass", "poison"],
"species": "seed",
"abilities": ["Overgrow", "Chlorophyll"],
"stats": {
"hp": 45,
"attack": 49,
"defense": 49,
"speed": 45
}
}
]
}
var data = require('./data.json');
var so = require('sort-object');
var sort = so.factory;
// define the value getters for each sort type
var types = {
name: 'name',
type: function(o) {
return (typeof o.types == 'array')
? o.types.sort()[0]
: o.types;
},
hp: function(o) { return o.stats.hp; },
speed: function(o) { return o.stats.speed; },
attack: function(o) { return o.stats.attack; },
defense: function(o) { return o.stats.defense; }
}
so.configure(types);
// create a new sorter
var PokemonSorter = sort(data.pokemon);
var sortedBySpeedDescending = PokemonSorter
// sort the items by the speed sort type
.sortBy(so.types.sort.speed)
// order the items in descending order
.order(so.types.order.descending)
// get the value of the sorted list
.value()
// => items sorted by stats.speed in descending order
var isNumeric = function(n) {
return !isNaN(parseFloat(n)) && isFinite(n);
};
var isString = function(s) {
return (typeof s == 'string');
};
var isArray = function(a) {
return (typeof a == 'array');
};
var Sort = {
types: {
sort: {},
order: {
ascending : 'ascending',
descending : 'descending'
}
},
methods: {
sort: {},
order: {
ascending : function(a) { return a; },
descending : function(a) { return a.reverse(); }
},
},
factory: function(array) {
return Object.create(Sort, { list: { value: array || [] } });
},
configure: function(types) {
Object.keys(types).map(function(key) {
if (isString(types[key])) {
types[key] = Sort._generateValueGetter(types, types[key]);
}
Sort.types.sort[key] = key;
Sort.methods.sort[key] = Sort._generateSortFn(types[key]);
}, this);
},
order: function(type) {
this.methods.order[type](this.list);
return this;
},
sortBy: function(type) {
this.list.sort(this.methods.sort[type]);
return this;
},
value: function() {
return this.list;
},
_generateValueGetter: function(object, key) {
return function(object) {
return object[key];
}
},
_generateSortFn: function(getValue) {
return function(a, b) {
var a = Sort._normalizeValue(getValue(a));
var b = Sort._normalizeValue(getValue(b));
if (isNumeric(a) && isNumeric(b)) return a - b;
if (a > b) return 1;
if (a < b) return -1;
return 0;
}
},
_normalizeValue: function(val) {
if (isArray(val)) val.sort();
if (isArray(val) && val.length === 1) return val[0];
return val;
}
};
module.exports = Sort;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment