Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid
 
PerlMonks  

Re^5: Sorting an array of hashes with an exeption

by choroba (Cardinal)
on May 10, 2013 at 16:11 UTC ( [id://1032978]=note: print w/replies, xml ) Need Help??


in reply to Re^4: Sorting an array of hashes with an exeption
in thread Sorting an array of hashes with an exception

I probably still do not understand how you want the results sorted. Can you give a more detailed description?
لսႽ† ᥲᥒ⚪⟊Ⴙᘓᖇ Ꮅᘓᖇ⎱ Ⴙᥲ𝇋ƙᘓᖇ
  • Comment on Re^5: Sorting an array of hashes with an exeption

Replies are listed 'Best First'.
Re^6: Sorting an array of hashes with an exeption
by martin_87 (Initiate) on May 10, 2013 at 16:29 UTC

    Sorry if I made the last post seem angry but I'm tired and a bit frustrated atm. Not my intention at all to sound mad at you directly.

    So the problem that I have is the following. I'm receiving an hash from an XMLpost. This hash contains alot of information but the only parts that I need is ID, Name, Distance and RouteDistance. I have not mentined Name before but it does not make any differance in this sorting problem that I got. This post needs to be sorted before presented. This sort should be easy if it wasn't for the cases where RouteDistance is zero. RouteDistance is the priority element that this sort should be done on but in those cases where RouteDistance is zero, the fallback is to match the Distance for the RouteDistance instead (but only the one which is zero). Then the sorting continues on RouteDistance until a new RouteDistance with zero arraives and in that case, same thing, sort this in on distance instead.

    The XMLpost contains of a maximum of 20 hashes so there is room fore some bad performance sorting aswell. Ofcourse a fast and clean solution would be the best but as this point I'm open for any soltion that would solve this problem.

    As I wrote in the "uptate 1" post in the original post I can accept an solution where the RouteDistance value is modified by for example + 0.01 / - 0.01 (depending how the loop is iterating over the array) with the nearest RouteDistance.

    I do find my example tables informative where I first wrote the original data and then what I expect after the sort is done.

    If you need more information you let me know, I find this a bit hard to describe, escpecially since I'm not that good at English

      That is what my code does. It sorts by RouteDistance, but in case it is zero, it is replaced with Distance. Your example, though, uses Distance even if the RouteDistance is zero on the other element being compared to. This is generally not possible, consider:
      ID => 1, Distance => 10, RouteDistance => 1, ID => 2, Distance => 9, RouteDistance => 0, ID => 3, Distance => 8, RouteDistance => 2,

      According to your logic, ID1 < ID3 (1<2), but ID3 < ID2 (8<9) and ID2 < ID1 (9<10), therefore ID3 < ID1. Your order is not transitive.

      لսႽ† ᥲᥒ⚪⟊Ⴙᘓᖇ Ꮅᘓᖇ⎱ Ⴙᥲ𝇋ƙᘓᖇ

        But I don't want to replace RouteDistance with the value of Distance. I want to sort it by Distance (match with the other Distance values that the hashes got) in cases where RouteDistance is zero.

        Anyhow, the problem is now solved.

        Thanks for helping out, Choroba.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1032978]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others admiring the Monastery: (6)
As of 2024-04-23 18:21 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found