Last active
August 29, 2015 14:02
-
-
Save neilhawkins/c6160730b2cebefac446 to your computer and use it in GitHub Desktop.
Ways of sorting an array of objects
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Ways to sort an array of objects | |
var arr = [{"member_id":"40323","person_id":"24709","name":"Bridget Phillipson","party":"Labour","constituency":"Houghton and Sunderland South","office":[]},{"member_id":"40636","person_id":"11592","name":"Sharon Hodgson","party":"Labour","constituency":"Washington and Sunderland West","office":[]},{"member_id":"40592","person_id":"24710","name":"Julie Elliott","party":"Labour","constituency":"Sunderland Central","office":[]},{"member_id":"40068","person_id":"13814","name":"Naomi Long","party":"Alliance","constituency":"Belfast East","office":[]},{"member_id":"40428","person_id":"13852","name":"Ian Paisley Jnr","party":"DUP","constituency":"North Antrim"},{"member_id":"40653","person_id":"10858","name":"Pat Doherty","party":"Sinn Fein","constituency":"West Tyrone"},{"member_id":"40434","person_id":"11003","name":"Kevan Jones","party":"Labour","constituency":"North Durham","office":[]},{"member_id":"40606","person_id":"10626","name":"Steve Webb","party":"Liberal Democrat","constituency":"Thornbury and Yate","office":[]},{"member_id":"40196","person_id":"24711","name":"Jenny Chapman","party":"Labour","constituency":"Darlington","office":[]},{"member_id":"40350","person_id":"10172","name":"Jeffrey M Donaldson","party":"DUP","constituency":"Lagan Valley"},{"member_id":"40045","person_id":"11323","name":"Hywel Williams","party":"Plaid Cymru","constituency":"Arfon","office":[]},{"member_id":"40347","person_id":"24713","name":"Chris Skidmore","party":"Conservative","constituency":"Kingswood"},{"member_id":"40129","person_id":"11461","name":"Charles Walker","party":"Conservative","constituency":"Broxbourne"}] | |
// Sort by id string - parsed to integer | |
arr.sort(function(a, b) { | |
// a - b for integer values | |
return parseInt(a.member_id, 10) - parseInt(b.member_id, 10); | |
}); | |
// Sort alphabetically by name (or any other string property) | |
arr.sort(function(a, b) { | |
var a = a.name.toLowerCase(), b = b.name.toLowerCase(); | |
// if a less than b, sort a to lower index than b | |
if (a < b) { | |
return -1; | |
} | |
// if b greater than a, sort b to a lower index than a | |
if (a > b) { | |
return 1; | |
} | |
// if a === b, leave unchanged | |
return 0; | |
// or using ternary operator | |
// return a < b ? -1 : a > b ? 1 : 0; | |
}); | |
// Using .map - runs function over each element in array | |
// and constructs new array from transformed values | |
var map = arr.map(function(e, i) { | |
// extracts constituency value from each object in array and gives it an index | |
return { index: i, value: e.constituency.toLowerCase() } | |
}) | |
// map = [ {index: 0, value: "houghton and sunderland south"}, {index: 1, value: "washington and sunderland west"}, { etc | |
// now sort map into order | |
map.sort(function (a,b) { | |
if (a.value < b.value) { | |
return -1; | |
} | |
if (a.value > b.value) { | |
return 1; | |
} | |
return 0; | |
}); | |
// map ordered by value not index | |
// map = [ {index: 10, value: "arfon"}, {index: 3, value: "belfast east"}, { etc | |
// use map to order the original array of objects put it in a new variable, sorted_array | |
var sorted_array = map.map(function(e) { | |
// the first item in map has index:10 so the first item in the sorted_array will be arr[10] | |
// which is {member_id: "40045", person_id: "11323", name: "Hywel Williams", party: "Plaid Cymru", constituency: "Arfon"…} | |
return arr[e.index]; | |
}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment