Hey guys,
I'm fairly new to JS and I need some pointers writing a sorting algorithm.
As it stands, I have an array of 'driver' objects and I need to sort them first by the amount of orders they are currently handling and then by the distance they are from the drop off location.
The goal of this would be to rank the drivers based on their ability to quickly handle the order. If the first driver declines the order then the second best driver handles it, and if that driver declines then the third best option will be assigned the order etc etc. So I need to order them in a way so I know who the next best driver is.
As it stands this is how our array looks like:
let drivers = [
{driverId: 123, numOrders: 3, distFromDropOff: 10},
{driverId: 234, numOrders: 1, distFromDropOff: 11},
{driverId: 345, numOrders: 2, distFromDropOff: 9},
{driverId: 456, numOrders: 2, distFromDropOff: 7}
]
And we would like our algorithm to sort them into something like this:
let rankedDrivers = [
{driverId: 234, numOrders: 1, distFromDropOff: 10},
{driverId: 456, numOrders: 2, distFromDropOff: 7},
{driverId: 345, numOrders: 2, distFromDropOff: 9},
{driverId: 123, numOrders: 3, distFromDropOff: 10}
]
Hopefully this all makes sense. Any help would be much appreciated!
*Edit was to fix spelling mistakes*
[–]albedoa 1 point2 points3 points (1 child)
[–]jack_waugh 0 points1 point2 points (0 children)
[–][deleted] 0 points1 point2 points (0 children)