Partial sorting

This commit is contained in:
James Barnsley
2016-12-09 18:47:10 +13:00
parent f89189c715
commit 26b069faa7
7 changed files with 69 additions and 17 deletions

View File

@ -243,4 +243,38 @@ export let createRange = function (indexes){
start: first_bunch[0],
length: first_bunch.length
}
}
/**
* Sort an array of objects
* @param array = array to sort
* @param property = string to sort by
* @param reverse = boolean
* @return array
**/
export let sortItems = function (array, property, reverse = false){
function compare(a,b) {
switch( typeof(a[property]) ){
case 'boolean':
return a[property]
break
default:
// both objects must have this property
if( typeof(a[property]) === 'undefined' || typeof(b[property]) === 'undefined' ) return 0
if(a[property] > b[property]) return 1
if(a[property] < b[property]) return -1
return 0
}
}
var sorted = array.sort(compare)
if( reverse ) sorted.reverse()
return sorted
}