Sort array of objects for both values and keys
Sort array of objects [{ key: value } ] first by value and then by key
by Yurii Predborskyi
HTML
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.5/lodash.min.js"></script>
<div id='wrapper'>
<div id='container'>
</div>
</div>
CSS
#wrapper {
font-family: monospace;
background-color: white;
}
JavaScript
function say(text) {
text = typeof text === typeof {} ? JSON.stringify(text) : text;
document.getElementById('container').innerHTML += text + '<br>';
}
let items = [
{ 100: 50 },
{ 175: 50 },
{ 150: 50 },
{ 300: 10 },
{ 50: 60 },
{ 325: 60 },
{ 275: 60 }
];
var obj = {Derp: 17, Herp: 2, Asd: 5, Foo: 8, Qwe: 12}
var result = _.reduceRight(_.invert(_.invert(obj)), function(current, val, key){
current[key] = parseInt(val);
return current;
},{});
say('result: ');
say(result);