douliang7068 2014-11-10 20:19
浏览 101
已采纳

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?

  • 写回答

1条回答 默认 最新

  • dongyan1625 2014-11-10 20:26
    关注

    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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 如何在3D高斯飞溅的渲染的场景中获得一个可控的旋转物体
  • ¥88 实在没有想法,需要个思路
  • ¥15 MATLAB报错输入参数太多
  • ¥15 python中合并修改日期相同的CSV文件并按照修改日期的名字命名文件
  • ¥15 有赏,i卡绘世画不出
  • ¥15 如何用stata画出文献中常见的安慰剂检验图
  • ¥15 c语言链表结构体数据插入
  • ¥40 使用MATLAB解答线性代数问题
  • ¥15 COCOS的问题COCOS的问题
  • ¥15 FPGA-SRIO初始化失败