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

时间:2018-04-16 22:53:40

标签: php php-5.4 usort

我想以复杂的方式对数组进行排序,并且不确定如何去做。以下是我正在使用的数据的粗略概念:

[
  { 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[...] },
  ...
]

我希望所有包含target的对象包含字符串cpu-pct,然后是target包含字符串memory的对象,然后{ {1}}。此数组可以包含任意数量的项目,因此按索引重新排序将不起作用。可能有1个example的对象包含target,或者可能有50个以上。我正在排序的其他字符串也一样。

我考虑循环遍历原始数组,检查是否存在所需的字符串,将匹配的对象保存到我正在寻找的每个目标的新数组中,然后在最后合并数组。我认为这样可行,但我认为有一个更好,更有效的解决方案,可能使用cpu-pct,但我不知所措。有什么想法吗?

2 个答案:

答案 0 :(得分:2)

memset(result, 0, 20); 绝对是最佳选择。这是一种相当普遍的做法,您可以扩展usort数组以包含其他术语,以便在您喜欢的情况下进行排序。

$ranks

输出:

$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);

如果要对关键字后面的值进行进一步排序,可以将其附加到排名值,然后对组合值进行排序,例如

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
                )

        )

)

如果要排序的字符串超过10个,则需要将返回值从$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"); 更改为rank以确保排序正常工作(将02替换为必要的数字以确保你可以用很多数字代表所有的排序字符串。)

答案 1 :(得分:1)

这是一种紧凑型方法,您必须为要排序的每种可能组合定义条件。希望我的代码注释会给你一个关于方法的提示。

$array = json_decode('[
  { "target": "random.text.cpu-pct-0" },
  { "target": "random.text.cpu-pct-1" },
  { "target": "random.text.cpu-pct-2"},
  { "target": "random.text.example-0" },
  { "target": "random.text.example-1" },
  { "target": "random.text.memory" }
]');

function mySortFunction( $one, $two )
{

  $pattern = '/\.(?<label>cpu-pct|example|memory)(?:-(?<value>\d+))?/';
  preg_match( $pattern, $one->target, $targetOne );
  preg_match( $pattern, $two->target, $targetTwo );

  // Both have CPU-PCT? then sort on CPU-PCT-VALUE
  if( $targetOne['label'] === 'cpu-pct' and $targetTwo['label'] === 'cpu-pct' )
  {
    return strcmp( $targetOne['value'], $targetTwo['value'] );
  }
  // Both have MEMORY? they are the same
  if( $targetOne['label'] === 'memory' and $targetTwo['label'] === 'memory' )
  {
    return 0;
  }
  // 1 has CPU but 2 has Memory, prefer CPU
  if( $targetOne['label'] === 'cpu-pct' and $targetTwo['label'] === 'memory' )
  {
    return -1;
  }
  // 1 has MEMORY but 2 has CPI, prefer CPU
  if( $targetOne['label'] === 'memory' and $targetTwo['label'] === 'cpu-pct' )
  {
    return 1;
  }
  // 1 is MEMORY or CPU, but 2 is Neither
  if( $targetOne['label'] === 'cpu-pct' or $targetOne['label'] === 'memory' )
  {
    if( $targetTwo['label'] !== 'cpu-pct' and $targetTwo['label'] !== 'memory' )
    {
      return -1;
    }
  }
  // 2 is MEMORY or CPU, but 1 is Neither
  if( $targetTwo['label'] === 'cpu-pct' or $targetTwo['label'] === 'memory' )
  {
    if( $targetOne['label'] !== 'cpu-pct' and $targetOne['label'] !== 'memory' )
    {
      return 1;
    }
  }
  // ETC
  // ETC
  // ETC
}

usort( $array, 'mySortFunction' );