PHP uasort没有排序数组
I have an array of values ($arrayToSort) that is as 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 well as my self defined sorting function
private function arraySort($a, $b) {
if ($a == $b) {
return 0;
}
return ($a < $b) ? -1 : 1;
}
When I call my sort function with the above dataset and function using uasort($arrayToSort,array('self','arraySort));
I get back my data in the exact same order. I have tried using my comparison functon with usort, and I get back the values sorted (albeit without the keys of course). For the life of me I cannot see why uasort is failing to sort these values when usort is capable of doing so. Am I calling the function incorrectly?
我有一个值数组($ arrayToSort),如下所示 p>
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 \ n28fb81b0-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
b 1d3a6a6-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
code> pre>
以及我自定义的排序功能 p>
private function arraySort($ a,$ b){
if($ a == $ b){
return 0;
}
return($ a&lt; $ b)? -1:1;
}
code> pre>
当我使用 uasort($ arrayToSort,array('self)调用带有上述数据集和函数的sort函数时 ','arraySort)); code>我以完全相同的顺序取回我的数据。 我已经尝试使用我的比较函数与usort,我得到了排序的值(尽管没有键当然)。 对于我的生活,我无法理解为什么当usort能够这样做时,uasort未能对这些值进行排序。 我是否错误地调用了该函数? p>
div>
Your call to uasort
seems wrong compared to the code you provided.
If you want to call uasort
using a standalone callback, you should use uasort($array, 'arraySort');
If you want to call uasort
from within an object instance, while the callback is an instance method, you should use uasort($array, array($this, 'arraySort'))
;
(cause your method is private i assume THIS is the call you need)
If you want to call uasort
from within an object instance, while the callback is a static method of the current class, then you should use uasort($arrayToSort,array('self','arraySort'));
Additionally you can simplify your sort method like this:
private function arraySort($a, $b) {
return strcmp($a, $b);
}
According to your answer, and assuming that the examples listed above are "strings", you need to split the values first, before sorting to get access to the actuall integer at the end of the string.
using uasort($array, array($this, 'arraySort'))
in your class, along with
private function arraySort($a, $b) {
$intOfA = explode(":", $a);
$intOfA = intval(trim($intOfA[1]));
$intOfB = explode(":", $b);
$intOfB = intval(trim($intOfB[1]));
return $intOfA - $intOfB;
}
should do the trick.