in reply to Sorting an array of hashes with an exception

Have you considered that this problem might not have a solution for some inputs? At least not with the rules that you have given us (as I understood them).

Consider these data records, already pre-sorted according to route distance.

ID = 5, Distance = 1,6, RouteDistance = 0

ID = 1, Distance = 1.7, RouteDistance = 2.5

ID = 2, Distance = 1.5, RouteDistance = 2.8

ID = 3, Distance = 2.3, RouteDistance = 4.3

IDs 1 2 and 3 are well sorted and need to stay in this order. Then, of course, the first line needs to be moved to be inserted somewhere else according to the Distance. But where are you going to insert it? Because IDs 1, 2 and 3 are not ordered for distance, it is not possible to insert ID 5 anywhere. For example, if you put it after ID 2, OK, it will be OK compared to ID 2 and 3, but it will be after ID 1, although its distance is smaller. Any other solution will fail likewise.

  • Comment on Re: Sorting an array of hashes with an exeption

Replies are listed 'Best First'.
Re^2: Sorting an array of hashes with an exeption
by martin_87 (Initiate) on May 08, 2013 at 20:44 UTC
    Hi Laurent_R,

    When using your example data I get the following result:

    ID: 2 Distance: 1.5 RouteDistance: 2.8
    ID: 5 Distance: 1.6 RouteDistance: 0
    ID: 1 Distance: 1.7 RouteDistance: 2.5
    ID: 3 Distance: 2.3 RouteDistance: 4.3

    As you thought this is not what I want, hmm. Need to think something else out.