1

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"
    }
  }
]
Dino Maria
  • 11
  • 2
  • 1
    you haven't `sourceindex` properties in your objects – Artyom Amiryan Dec 07 '18 at 12:18
  • @ArtyomAmiryan Its sourcelocation.orderindex , I updated, thanks for pointing out. – Dino Maria Dec 07 '18 at 12:20
  • Can anyone please help. Thanks. – Dino Maria Dec 07 '18 at 12:36
  • More people will help if you make an attempt first rather than just asking for the solution, start with something like [inputSet.forEach()](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/forEach) to iterate over your data, copying each item to a new array – Lloyd Dec 07 '18 at 12:40
  • @Lloyd I experimented with it and trying to get solution since last 2 days, Intresting thing is you cant do it with just a loop , you will need to take care of conditions as well which I mentioned earlier. Thanks – Dino Maria Dec 07 '18 at 12:50
  • This is not complete solution but this thread may help you. https://stackoverflow.com/questions/1069666/sorting-javascript-object-by-property-value – Wajid Dec 07 '18 at 12:51
  • sorting is a very common, very old problem, and yes you will use a loop, conditions, and probably recursion depending on your method of choice, sorting is something you definitely want to spend some time learning from start to finish. – Lloyd Dec 07 '18 at 12:56
  • @Lloyd I did all of that but doing something fishy which fails for certain cases, When I felt hopeless then I headed to stackoverflow community, Hope someone can help me in figuring out the solution. Thanks – Dino Maria Dec 07 '18 at 12:59

0 Answers0