I have an object that looks like the one below. How can I sort something like this based on a common property within nested object. The output I expect is for player2 to come first based on the higher score.
My challenge is in accessing the property of each object to sort.
Here is what I had in mind and tried but it didn't do the sorting.
Object.keys(data).sort(function(p1, p2){
return p1.score - p2.score;
}).forEach(function(key) {
var value = data[key];
delete data[key];
data[key] = value;
});
My data
var data =
{
player1:
{ score: 4,
cards: 6 },
player2:
{ score: 6,
cards: 4}
}
I have an object that looks like the one below. How can I sort something like this based on a common property within nested object. The output I expect is for player2 to come first based on the higher score.
My challenge is in accessing the property of each object to sort.
Here is what I had in mind and tried but it didn't do the sorting.
Object.keys(data).sort(function(p1, p2){
return p1.score - p2.score;
}).forEach(function(key) {
var value = data[key];
delete data[key];
data[key] = value;
});
My data
var data =
{
player1:
{ score: 4,
cards: 6 },
player2:
{ score: 6,
cards: 4}
}
Share
Improve this question
edited Jan 23, 2018 at 19:59
GG.
21.8k14 gold badges90 silver badges132 bronze badges
asked Jan 23, 2018 at 19:51
mo_maatmo_maat
2,24014 gold badges46 silver badges82 bronze badges
1
- 5 The properties of a JavaScript object are unordered. If you want values in a specific order, you need to put them in an array. – Jordan Running Commented Jan 23, 2018 at 19:55
3 Answers
Reset to default 10You need to sort the data with the object, not with a key's property and then it has to be reverted, because you need a descending sort.
return data[b].score - data[a].score;
// ^^^^^^^ ^^^^^^^ object
// ^ ^ descending
I suggest to use an empty object and insert the properties by the ordered keys.
var data = { player1: { score: 4, cards: 6 }, player2: { score: 6, cards: 4 } },
sorted = {};
Object
.keys(data).sort(function(a, b){
return data[b].score - data[a].score;
})
.forEach(function(key) {
sorted[key] = data[key];
});
console.log(sorted);
Here is one line functional approach using Object.entries()
, Array.prototype.sort()
and Object.fromEntries
method. Before sorting you need to make the object an array by using Object.entries()
method. It returns array of key-value pair of the given object. Then sort the array in descending order by the score. At last, use Object.fromEntries()
method to transform the key-value pair into an object.
const data = {
player1: { score: 4, cards: 6 },
player2: { score: 6, cards: 4 },
};
const ret = Object.fromEntries(
Object.entries(data).sort((x, y) => y[1].score - x[1].score)
);
console.log(ret);
You can't sort an object. You should convert your object to an array and then sort it.
var data =
{
player1:
{ score: 4,
cards: 6 },
player2:
{ score: 6,
cards: 4}
}
var array = $.map(data, function(value, index) {
value.key = index;
return value;
});
var sortedData = array.sort(function(p1, p2){
return p2.score - p1.score;
});
console.log(sortedData);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>