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 }
];
let newItems = items.map(item=>({price: _.keys(item)[0], repeats: _.values(item)[0]})).sort((a, b) => b.repeats - a.repeats);
let maxRepeats = newItems[0].repeats;
let mostCommonMax = newItems.filter(item=>item.repeats >= maxRepeats).sort((a, b) => b.price - a.price)[0].price;
say('most common max is:');
say(mostCommonMax);