javascript - sorting object by keys (strings as numbers) -
i have data set in object, keys strings
{ '17': '17-17:30', '20': '20:00-21', '21': '21-22', '22': '22-23', '23': '23-24', '01': '1-2', '02': '2-3', '03': '3-4', '04': '4-5', '05': '5-6', '06': '6-7', '07': '7-7:30', '08': '08:50-9' }
i want arrange them numerically 01 comes first , 23 comes last.
here's code i'm using:
var sort_object = function(map) { var keys = _.sortby(_.keys(map), function(a) { return number(a); }); var newmap = {}; _.each(keys, function(k) { newmap[k] = map[k]; }); return newmap; }
it still returns 17 in beginning. did go wrong?
as blender pointed out, objects not ordered. need use array. there several ways set array, , once have data in array it's easy sort it.
here couple of examples. first, let's try making array of arrays. outer array list of elements, , each element of array array of 2 elements, key , value.
so paste code favorite javascript console, such 1 in chrome:
var data = [ [ '17', '17-17:30' ], [ '20', '20:00-21' ], [ '21', '21-22' ], [ '22', '22-23' ], [ '23', '23-24' ], [ '01', '1-2' ], [ '02', '2-3' ], [ '03', '3-4' ], [ '04', '4-5' ], [ '05', '5-6' ], [ '06', '6-7' ], [ '07', '7-7:30' ], [ '08', '08:50-9' ] ]; var result = data.slice().sort( function( a, b ) { return +a[0] - +b[0]; }); json.stringify( result, null, 4 );
it log:
[ [ "01", "1-2" ], [ "02", "2-3" ], [ "03", "3-4" ], [ "04", "4-5" ], [ "05", "5-6" ], [ "06", "6-7" ], [ "07", "7-7:30" ], [ "08", "08:50-9" ], [ "17", "17-17:30" ], [ "20", "20:00-21" ], [ "21", "21-22" ], [ "22", "22-23" ], [ "23", "23-24" ]
or, instead of array of arrays, can use array of objects. more convenient work with. paste code javascript console:
var data = [ { key: '17', value: '17-17:30' }, { key: '20', value: '20:00-21' }, { key: '21', value: '21-22' }, { key: '22', value: '22-23' }, { key: '23', value: '23-24' }, { key: '01', value: '1-2' }, { key: '02', value: '2-3' }, { key: '03', value: '3-4' }, { key: '04', value: '4-5' }, { key: '05', value: '5-6' }, { key: '06', value: '6-7' }, { key: '07', value: '7-7:30' }, { key: '08', value: '08:50-9'} ]; var result = data.slice().sort( function( a, b ) { return +a.key - +b.key; }); json.stringify( result, null, 4 );
it log:
[ { "key": "01", "value": "1-2" }, { "key": "02", "value": "2-3" }, { "key": "03", "value": "3-4" }, { "key": "04", "value": "4-5" }, { "key": "05", "value": "5-6" }, { "key": "06", "value": "6-7" }, { "key": "07", "value": "7-7:30" }, { "key": "08", "value": "08:50-9" }, { "key": "17", "value": "17-17:30" }, { "key": "20", "value": "20:00-21" }, { "key": "21", "value": "21-22" }, { "key": "22", "value": "22-23" }, { "key": "23", "value": "23-24" } ]
either way, can see, once have data in suitable format (i.e. data whole needs array), becomes simple sort it.
let's @ sort function in more detail (using second example):
var result = data.slice().sort( function( a, b ) { return +a.key - +b.key; });
in code a.key
gets value key
property of 1 of array elements. +
in front of converts string number - using number
function simpler way it. +b.key
. , subtracting 2 numbers gives correct sort function return: positive, negative, or 0 value depending on whether a.key
or b.key
greater of 2 or if they're equal.
the first example works similarly, using +a[0] - +b[0]
instead of +a.key - +b.key
.
the .slice()
call in each example makes copy of array. omit if want sort original array.
Comments
Post a Comment