Answer the question
In order to leave comments, you need to log in
How to sort nested JS arrays?
Hello, I have such an array of nested data, I'm trying to sort it by the 'sorting' field. Everything is ok, but how to sort nested tournaments by the same field, and in tournaments sort events all by the same 'sorting' field? Please help, I already did everything I could.
data.sort(function (a, b) {
if (a.sorting > b.sorting) {
return -1;
}
if (a.sorting < b.sorting) {
return 1;
}
return 0;
});
Data array:
[
{
"id":67,
"name":{
"en":"Wrestling",
},
"sorting":25,
"tournaments":{
"1522":{
"id":1522,
"game_id":67,
"name_original":"MMA Fight Nights Global. MMA LFA",
"name_lang":{
"en":"MMA Fight Nights Global. MMA LFA",
},
"sorting":1,
"events":{
"187":{
"id":187,
"game_id":67,
"tournament_id":1522,
"date_start":"2019-09-14 01:00:00",
"sorting":2002233540,
"status":1,
"home_team_name":"Chris Harris",
"away_team_name":"Buckley J",
"IsActive":true,
},
"190":{
"id":190,
"game_id":67,
"tournament_id":1522,
"date_start":"2019-09-14 01:30:00",
"sorting":2002233544,
"status":1,
"home_team_name":"Ogden T",
"away_team_name":"Nick Browne",
"IsActive":true,
}
}
},
.............. Турнир 2, 3 и т.д
},
},
............. Игра 2, 3 и т.д
]
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question