I am trying to reorder following array. Actually sorting is
based on the sourcelocation.orderindex
, every object has its corresponding
another object (with rider_id
same) along with is_dropoff
field true
, I want to reorder the array in the following way.
Step 1: Objects for which sourceindex
is 0
should be on top, in our
case top 3 object will be on top now next object must have sourcelocation.order
greater then 0
and is_dropoff
true please note destinationlocation.orderindex
must not be greater then forthcoming objects sourcelocation.orderindex
value.
let inputSet = [
{
"sourcelocation": {
"orderindex": "0"
},
"destinationlocation": {
"orderindex": "1"
},
"rider_ids": "2b7116ea3dead9870b828a1v"
},
{
"sourcelocation": {
"orderindex": "0"
},
"destinationlocation": {
"orderindex": "1"
},
"rider_ids": "4b7116ea3dead9870b828a19"
},
{
"sourcelocation": {
"orderindex": "0"
},
"destinationlocation": {
"orderindex": "3"
},
"rider_ids": "548e653d56060c83838b772e"
},
{
"rider_ids": "2b7116ea3dead9870b828a1v",
"is_dropoff": true,
"address": {
"orderindex": "1"
}
},
{
"rider_ids": "4b7116ea3dead9870b828a19",
"is_dropoff": true,
"address": {
"orderindex": "1"
}
},
{
"rider_ids": "548e653d56060c83838b772e",
"is_dropoff": true,
"address": {
"orderindex": "3"
}
},
{
"sourcelocation": {
"orderindex": "2"
},
"destinationlocation": {
"orderindex": "3"
},
"rider_ids": "5b8e676d56060c83838b772e"
},
{
"rider_ids": "5b8e676d56060c83838b772e",
"is_dropoff": true,
"address": {
"orderindex": "3"
}
}
]
The following must be the response that I am expecting
let outputSet = [
{
"sourcelocation": {
"orderindex": "0"
},
"destinationlocation": {
"orderindex": "1"
},
"rider_ids": "2b7116ea3dead9870b828a1v"
},
{
"sourcelocation": {
"orderindex": "0"
},
"destinationlocation": {
"orderindex": "1"
},
"rider_ids": "4b7116ea3dead9870b828a19"
},
{
"sourcelocation": {
"orderindex": "0"
},
"destinationlocation": {
"orderindex": "3"
},
"rider_ids": "548e653d56060c83838b772e"
},
{
"rider_ids": "2b7116ea3dead9870b828a1v",
"is_dropoff": true,
"address": {
"orderindex": "1"
}
},
{
"rider_ids": "4b7116ea3dead9870b828a19",
"is_dropoff": true,
"address": {
"orderindex": "1"
}
},
{
"sourcelocation": {
"orderindex": "2"
},
"destinationlocation": {
"orderindex": "3"
},
"rider_ids": "5b8e676d56060c83838b772e"
},
{
"rider_ids": "5b8e676d56060c83838b772e",
"is_dropoff": true,
"address": {
"orderindex": "3"
}
},
{
"rider_ids": "548e653d56060c83838b772e",
"is_dropoff": true,
"address": {
"orderindex": "3"
}
}
]