Topic awaiting preservation: Help with arrays... |
|
---|---|
Author | Thread |
Lunatic (VI) Inmate From: under the bed |
posted 07-09-2009 20:31
Good afternoon. |
Maniac (V) Mad Scientist with Finglongers From: Germany |
posted 07-10-2009 11:25
you need to write a comparison function that's passed the arrays, looks at the sub arrays, and returns -1, 0 or 1 depending on larger, equal or smaller (or maybe it's the other way around - I never can remember). |
Lunatic (VI) Inmate From: under the bed |
posted 07-13-2009 19:37
Ok, I am having a really tough time grasping what I need to actually do in the comparison function. code: Array ( [0] => Array ( [ves_id] => 96 [name] => VECCHIO BRIDGE [pol] => HKHKG [pod] => USNYC [etd] => 2009-06-07 [eta] => 2009-07-01 [place] => 25 ) [1] => Array ( [ves_id] => 95 [name] => HYUNDAI VOYAGER [pol] => TWKHH [pod] => USNYC [etd] => 2009-06-09 [eta] => 2009-07-02 [place] => 25 ) [2] => Array ( [ves_id] => 99 [name] => HANJIN VALENCIA [pol] => CNNGB [pod] => USNYC [etd] => 2009-06-14 [eta] => 2009-07-08 [place] => 25 ) [3] => Array ( [ves_id] => 100 [name] => VINCENT THOMAS BRIDGE [pol] => CNYTN [pod] => USNYC [etd] => 2009-06-15 [eta] => 2009-07-08 [place] => 25 ) [4] => Array ( [ves_id] => 101 [name] => ITAL LAGUNA [pol] => CNSHA [pod] => USNYC [etd] => 2009-06-16 [eta] => 2009-07-13 [place] => 25 ) [5] => Array ( [ves_id] => 103 [name] => VANCOUVER BRIDGE [pol] => CNYTN [pod] => USNYC [etd] => 2009-06-22 [eta] => 2009-07-14 [place] => 24 ) [6] => Array ( [ves_id] => 105 [name] => HANJIN MARSEILLES [pol] => CNSHA [pod] => USELZ [etd] => 2009-06-24 [eta] => 2009-07-15 [place] => 23 ) [7] => Array ( [ves_id] => 106 [name] => VERRAZANO BRIDGE [pol] => HKHKG [pod] => USNYC [etd] => 2009-06-29 [eta] => 2009-07-22 [place] => 15 ) [8] => Array ( [ves_id] => 109 [name] => HYUNDAI GOODWILL [pol] => TWKHH [pod] => USNYC [etd] => 2009-06-30 [eta] => 2009-07-23 [place] => 14 ) [9] => Array ( [ves_id] => 104 [name] => HANJIN DURBAN [pol] => TWKHH [pod] => USNYC [etd] => 2009-06-27 [eta] => 2009-07-24 [place] => 15 ) [10] => Array ( [ves_id] => 114 [name] => HANJIN PORTLAND [pol] => CNSHA [pod] => USEZA [etd] => 2009-07-06 [eta] => 2009-07-29 [place] => 8 ) [11] => Array ( [ves_id] => 110 [name] => VALENCIA BRIDGE [pol] => CNYTN [pod] => USNYC [etd] => 2009-07-06 [eta] => 2009-07-29 [place] => 8 ) [12] => Array ( [ves_id] => 112 [name] => YM TAICHUNG [pol] => TWKHH [pod] => USNYC [etd] => 2009-07-05 [eta] => 2009-07-31 [place] => 8 ) [13] => Array ( [ves_id] => 115 [name] => ITAL MILIONE [pol] => CNSHA [pod] => USNYC [etd] => 2009-07-07 [eta] => 2009-08-03 [place] => 6 ) [14] => Array ( [ves_id] => 111 [name] => OOCL OAKLAND [pol] => THBKK [pod] => USNYC [etd] => 2009-07-05 [eta] => 2009-08-04 [place] => 7 ) [15] => Array ( [ves_id] => 116 [name] => VIRGINIA BRIDGE [pol] => HKHKG [pod] => USNYC [etd] => 2009-07-12 [eta] => 2009-08-05 [place] => 1 ) )
|
Lunatic (VI) Inmate From: under the bed |
posted 07-13-2009 20:55
Ok...I am just not getting it |
Maniac (V) Mad Scientist with Finglongers From: Germany |
posted 07-14-2009 08:56
code: myArray = ... the stuff above... function sortByPlace(a, b) { if (a["place"] > b["place"]) return 1; else if (a["place"] < b["place"]) return -1; else return 0; } function cleverSortByPlace(a,b) { return a["place"] - b["place"]; } myArray.sort(cleverSortByPlace); |
Lunatic (VI) Inmate From: under the bed |
posted 07-14-2009 13:22
That's more or less what I was doing (based on the example in the manual), but without success. |
Maniac (V) Mad Scientist with Finglongers From: Germany |
posted 07-14-2009 15:18
right... php, not javascript . |
Lunatic (VI) Inmate From: under the bed |
posted 07-14-2009 17:24
Ok, well that just confuses me more |
Maniac (V) Mad Scientist with Finglongers From: Germany |
posted 07-15-2009 09:22
too elaborate, sorting can (like every mutating operation, actually) either |
Lunatic (VI) Inmate From: under the bed |
posted 07-15-2009 18:19
But... |
Maniac (V) Mad Scientist with Finglongers From: Germany |
posted 07-16-2009 10:22
quote:
code: $myArray = ... print_r(usort($myArray));
code: $myArray = ... usort($myArray); print_r($myArray);
|