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
| [reply] |
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.
| [reply] [d/l] |
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.
| [reply] |