Wednesday, 15 February 2012

PHP uasort not sorting array -



PHP uasort not sorting array -

i have array of values ($arraytosort) follows

0f03895b-3db8-4bf5-a1b1-62b47f5dab5c: 500 1af8cd11-e2ae-4bf9-8024-f0d4874b2d74: 1030 1d815a24-06de-4794-a3f0-7d38857fee4b: 2033 3c0454fc-e1e5-43b7-ac03-b746a36d06a1: 1034 3d78d082-c6b7-462e-8f7f-38acdd3de761: 2029 8cea7e76-9873-4a98-903f-2b4af3986796: 2250 9bbf7197-f167-4691-8194-2e8868070bb4: 500 9f1dcc1a-6561-411c-b1a7-6af19efe77f3: 2052 28fb81b0-7162-40fc-bb5b-c266f43f3593: 2214 35eed79a-f879-43fa-952b-806f047d66a8: 3038 41ed4161-077a-4370-a1fb-47ddd112b5df: 6076 50cb8711-0ffb-4da2-97d6-033a948ea7c5: 1030 80b0e919-054f-4d4b-b0b6-3a8e14647879: 1035 88c17b89-5b82-4348-ab03-47d4794412fd: 428 275fd6b6-b880-4e25-bd7d-cd609685e922: 1031 630b8187-edfb-44de-ae23-e81b670c9706: 29 700c8740-9cae-444b-b69c-ad4361bfaf81: 230 5414bfe7-4d9b-41e0-bf56-d6da7534db8c: 29 7262ccf0-09a3-450b-a258-6aa1c1072fb8: 4037 7486a107-390a-43a3-8cc5-b6de094d50cb: 600 b1d3a6a6-b107-4665-ad86-4bd827b8a76e: 29 b6487821-c823-4262-aaaf-e758d38e2826: 214 bfa88b65-3724-47d1-a3c6-055abd568d27: 2333 f47e1e05-da9a-40bf-9b0d-b8f86cbcd522: 2032 fd1bbd8d-21e9-44c3-8916-a54ee3a554cc: 2000

as self defined sorting function

private function arraysort($a, $b) { if ($a == $b) { homecoming 0; } homecoming ($a < $b) ? -1 : 1; }

when phone call sort function above dataset , function using uasort($arraytosort,array('self','arraysort)); info in exact same order. have tried using comparing functon usort, , values sorted (albeit without keys of course). life of me cannot see why uasort failing sort these values when usort capable of doing so. calling function incorrectly?

your phone call uasort seems wrong compared code provided.

if want phone call uasort using standalone callback, should utilize uasort($array, 'arraysort');

if want phone call uasort within object instance, while callback instance method, should utilize uasort($array, array($this, 'arraysort')); (cause method private assume phone call need)

if want phone call uasort within object instance, while callback static method of current class, then should utilize uasort($arraytosort,array('self','arraysort'));

additionally can simplify sort method this:

private function arraysort($a, $b) { homecoming strcmp($a, $b); }

according answer, , assuming examples listed above "strings", need split values first, before sorting access actuall integer @ end of string.

using uasort($array, array($this, 'arraysort')) in class, along

private function arraysort($a, $b) { $intofa = explode(":", $a); $intofa = intval(trim($intofa[1])); $intofb = explode(":", $b); $intofb = intval(trim($intofb[1])); homecoming $intofa - $intofb; }

should trick.

php arrays sorting

No comments:

Post a Comment