if I have a big collection of array, what is the fastest way to sort and return the first 5 items?
Array of Object:
[
{
"name" : "placeA",
"menus" : [
{
"sold" : 5,
"name" : "menuA"
},
{
"sold" : 20,
"_id" : "menuB"
},
{
"sold" : 1000,
"_id" : "menuC"
}
]
},
{
"name" : "placeB",
"menus" : [
{
"sold" : 300,
"name" : "menuD"
},
{
"sold" : 400,
"_id" : "menuE"
},
{
"sold" : 50,
"_id" : "menuF"
}
]
},
{
"name" : "placeC",
"menus" : [
{
"sold" : 1500,
"name" : "menuG"
},
{
"sold" : 450,
"_id" : "menuH"
},
{
"sold" : 75,
"_id" : "menuI"
}
]
}
]
Expected Output:
[
{
"sold" : 1500,
"name" : "menuG"
},
{
"sold" : 1000,
"_id" : "menuC"
},
{
"sold" : 450,
"_id" : "menuH"
},
{
"sold" : 400,
"_id" : "menuE"
},
{
"sold" : 300,
"name" : "menuD"
}
]
The only possible way that I'm able to think of is to create an array that is filled all the menus and then sort the menus and then slice the first 5 items. I don't think this way is good enough for sorting a huge collection of array in real use case.