Created
March 5, 2015 09:11
-
-
Save bultas/aeda3d6b123c61213509 to your computer and use it in GitHub Desktop.
Get Sorted Object Keys
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
/** | |
* Get array of sorted Object's keys | |
* @param {object} Object which keys do you want to sort | |
* @param {boolen} byValue - false/null if you want to sort Object keys by Object's keys | |
* @param {boolen} byValue - true if you want to sort Object keys by Object's properties value | |
* @param {string} byValue - string if you want to sort Object keys by value inside each Object's property | |
* @return {array} Array of sorted keys (strings) | |
*/ | |
var getSortedObjKeys = function(obj, byValue) { | |
if (!byValue) { | |
return Object.keys(obj).sort(); | |
} | |
var valuesWithKeyArray = []; | |
var sortedKeys = []; | |
for (var prop in obj) { | |
var newKey; | |
if (byValue !== true) { | |
newKey = obj[prop][byValue]; | |
} else { | |
newKey = obj[prop]; | |
} | |
valuesWithKeyArray.push([newKey, prop]); | |
} | |
valuesWithKeyArray.sort(); | |
for (var i = 0; i < valuesWithKeyArray.length; i++) { | |
sortedKeys.push(valuesWithKeyArray[i][1]); | |
} | |
return sortedKeys; | |
}; | |
module.exports = getSortedObjKeys; |
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
var assert = require("assert"); | |
var clone = require("source/utils/clone"); | |
var getSortedObjKeys = require("source/utils/getSortedObjKeys"); | |
var _values = ["a", "b", "c", "d"]; | |
var _objectWithStrings = { | |
d: _values[0], | |
c: _values[1], | |
b: _values[2], | |
a: _values[3] | |
}; | |
var _objectHaveKeysWithSameValue = { | |
d: _values[0], | |
c: _values[1], | |
b: _values[2], | |
a: _values[0] | |
}; | |
var _objectWithObjects = { | |
d: {name: _values[0]}, | |
a: {name: _values[3]}, | |
c: {name: _values[1]}, | |
b: {name: _values[2]} | |
}; | |
describe("getSortedObjKeys function", function() { | |
it('Sort Object keys by Object props', function() { | |
var values = clone(_values); | |
var sortedArray = getSortedObjKeys(_objectWithStrings); | |
assert.deepEqual(sortedArray, values); | |
sortedArray = getSortedObjKeys(_objectWithStrings, false); | |
assert.deepEqual(sortedArray, values); | |
}); | |
it("Sort Object keys by Object's props Values - object have keys with same values", function() { | |
var values = ["a", "d", "c", "b"]; | |
var sortedArray = getSortedObjKeys(_objectHaveKeysWithSameValue, true); | |
assert.deepEqual(sortedArray, values); | |
}); | |
it("Sort Object keys by Object's props Values", function() { | |
var values = clone(_values).reverse(); | |
var sortedArray = getSortedObjKeys(_objectWithStrings, true); | |
assert.deepEqual(sortedArray, values); | |
}); | |
it("Sort Object keys by Object's objects value", function() { | |
var values = clone(_values).reverse(); | |
var sortedArray = getSortedObjKeys(_objectWithObjects, "name"); | |
assert.deepEqual(sortedArray, values); | |
}); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment