dtkf64283 2018-04-16 22:53
浏览 54
已采纳

按非字母,用户定义的字符串值对数组进行排序

I want to sort an array in a complicated way and am not sure how to go about it. Here is a rough idea of the data I'm working with:

[
  { target: random.text.cpu-pct-0, otherData[...] },
  { target: random.text.cpu-pct-1, otherData[...] },
  { target: random.text.cpu-pct-2, otherData[...] },
  { target: random.text.example-0, otherData[...] },
  { target: random.text.example-1, otherData[...] },
  { target: random.text.memory, otherData[...] },
  ...
]

I want all the objects with a target that includes the string cpu-pct to come first, then the objects with a target that includes the string memory, then example. This array could have any number of items, so re-sorting by index won't work. There could be 1 object with a target that includes cpu-pct, or there could be 50+. Same goes for the other strings I'm sorting by.

I thought about looping over the original array, checking if the desired string exists, saving the matching objects to new arrays for each target I'm looking for, then merging the arrays at the end. I think that would work, but I'd imagine there is a better, more efficient solution, probably using usort, but I'm at a loss. Any ideas?

  • 写回答

2条回答 默认 最新

  • dtstnjl898781429 2018-04-16 23:17
    关注

    usort is definitely the way to go. Here's a fairly generalised way of doing it, you can expand the $ranks array to include other terms to sort against if you like.

    $a = json_decode('[
      { "target": "random.text.cpu-pct-0", "otherData": [1, 2, 3] },
      { "target": "random.text.cpu-pct-1", "otherData": [2, 2, 3] },
      { "target": "random.text.cpu-pct-2", "otherData": [3, 2, 3] },
      { "target": "random.text.example-0", "otherData": [4, 2, 3] },
      { "target": "random.text.example-1", "otherData": [5, 2, 3] },
      { "target": "random.text.memory", "otherData": [6, 2, 3] } ]');
    
    $ranks = array('example' => 0, 'memory' => 1, 'cpu-pct' => 2);
    
    function rank($obj) {
        global $ranks;
    
        foreach ($ranks as $key => $value) {
            if (strpos($obj->target, $key) !== false) return $value;
        }
        // sort anything that doesn't match last
        return -1;
    }
    
    function cmp($a, $b) {
        return rank($b) - rank($a);
    }
    
    usort($a, "cmp");
    
    print_r($a);
    

    Output:

    Array
    (
        [0] => stdClass Object
            (
                [target] => random.text.cpu-pct-0
                [otherData] => Array
                    (
                        [0] => 1
                        [1] => 2
                        [2] => 3
                    )
    
            )
    
        [1] => stdClass Object
            (
                [target] => random.text.cpu-pct-1
                [otherData] => Array
                    (
                        [0] => 2
                        [1] => 2
                        [2] => 3
                    )
    
            )
    
        [2] => stdClass Object
            (
                [target] => random.text.cpu-pct-2
                [otherData] => Array
                    (
                        [0] => 3
                        [1] => 2
                        [2] => 3
                    )
    
            )
    
        [3] => stdClass Object
            (
                [target] => random.text.memory
                [otherData] => Array
                    (
                        [0] => 6
                        [1] => 2
                        [2] => 3
                    )
    
            )
    
        [4] => stdClass Object
            (
                [target] => random.text.example-0
                [otherData] => Array
                    (
                        [0] => 4
                        [1] => 2
                        [2] => 3
                    )
    
            )
    
        [5] => stdClass Object
            (
                [target] => random.text.example-1
                [otherData] => Array
                    (
                        [0] => 5
                        [1] => 2
                        [2] => 3
                    )
    
            )
    
    )
    

    If you want to further sort on the value after the keyword, you could append that to the ranking value and then sort on the combined value e.g.

    $ranks = array('cpu-pct', 'memory', 'example');
    
    function rank($obj) {
        global $ranks;
    
        foreach ($ranks as $key => $value) {
            if (preg_match("/$value(.*)$/", $obj->target, $matches))
                return $key . $matches[1];
        }
        // sort anything that doesn't match last
        return 'z';
    }
    
    function cmp($a, $b) {
        return strcmp(rank($a), rank($b));
    }
    
    usort($a, "cmp");
    

    If you had more than 10 strings to sort by, you would need to change the return value from rank to sprintf('%02d', $key) . $matches[1] to ensure sorting worked properly (replace 02 with as many digits as necessary to ensure you can represent all the sort strings in that many digits).

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 乌班图ip地址配置及远程SSH
  • ¥15 怎么让点阵屏显示静态爱心,用keiluVision5写出让点阵屏显示静态爱心的代码,越快越好
  • ¥15 PSPICE制作一个加法器
  • ¥15 javaweb项目无法正常跳转
  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 java 的protected权限 ,问题在注释里
  • ¥15 这个是哪里有问题啊?