【问题标题】:How would I sort an Array of Std Class Objects by specific listing order?如何按特定的列表顺序对 Stdclass 对象数组进行排序?
【发布时间】:2018-02-11 10:44:56
【问题描述】:

我有一个 StdClass 对象数组,其中包含按价格排序的几种不同汽车类型(例如紧凑型汽车、小型货车等)。我想按以下顺序对数组进行排序:

  • 经济型车
  • 小型车
  • 中型车
  • 标准车
  • 全尺寸汽车
  • 高级轿车
  • 豪华轿车
  • 标准尺寸敞篷车
  • 中型SUV
  • 标准SUV
  • 全尺寸SUV
  • 小型货车
  • 按价格排序的剩余汽车

我不确定使用键“描述”或“代码”对该订单进行排序是否有意义。我已经想出了如何按价格排序,但不确定如何按照上面列出的顺序重新排序数组。任何帮助,将不胜感激。谢谢!

输入数组:

    array (
    0 => 
    stdClass::__set_state(array(
       'description' => 'Mid-Size SUV',
       'codes' => 
      array (
        0 => 'IFAR',
      ),
       'max_people' => '5',
       'min_people' => '5',
       'max_bags' => '4',
       'min_bags' => '1',
       'price' => '30.31',
    )),
    1 => 
    stdClass::__set_state(array(
       'description' => 'Standard-Size SUV',
       'codes' => 
      array (
        0 => 'SFAR',
        1 => 'RFAR',
      ),
       'max_people' => '7',
       'min_people' => '5',
       'max_bags' => '4',
       'min_bags' => '1',
       'price' => '35.53',
    )),
    2 => 
    stdClass::__set_state(array(
       'description' => 'Specialty Vehicle',
       'codes' => 
      array (
        0 => 'XXAR',
      ),
       'max_people' => false,
       'min_people' => false,
       'max_bags' => '',
       'min_bags' => '',
       'price' => '36.72',
    )),
    3 => 
    stdClass::__set_state(array(
       'description' => 'Compact Car',
       'codes' => 
      array (
        0 => 'CCAR',
        1 => 'CDAR',
      ),
       'max_people' => '5',
       'min_people' => '4',
       'max_bags' => '3',
       'min_bags' => '1',
       'price' => '37.21',
    )),
    4 => 
    stdClass::__set_state(array(
       'description' => 'Economy Car',
       'codes' => 
      array (
        0 => 'ECAR',
        1 => 'EDAR',
      ),
       'max_people' => '5',
       'min_people' => '4',
       'max_bags' => '2',
       'min_bags' => '1',
       'price' => '37.21',
    )),
    5 => 
    stdClass::__set_state(array(
       'description' => 'Mid-Size Car',
       'codes' => 
      array (
        0 => 'ICAR',
        1 => 'IDAR',
      ),
       'max_people' => '5',
       'min_people' => '5',
       'max_bags' => '4',
       'min_bags' => '1',
       'price' => '39.46',
    )),
    6 => 
    stdClass::__set_state(array(
       'description' => 'Standard-Size Car',
       'codes' => 
      array (
        0 => 'SCAR',
        1 => 'SDAR',
      ),
       'max_people' => '5',
       'min_people' => '5',
       'max_bags' => '4',
       'min_bags' => '1',
       'price' => '41.77',
    )),
    7 => 
    stdClass::__set_state(array(
       'description' => 'Minivan',
       'codes' => 
      array (
        0 => 'MVAR',
      ),
       'max_people' => '7',
       'min_people' => '7',
       'max_bags' => '5',
       'min_bags' => '2',
       'price' => '43.18',
    )),
    8 => 
    stdClass::__set_state(array(
       'description' => 'Full-Size Car',
       'codes' => 
      array (
        0 => 'FCAR',
        1 => 'FDAR',
      ),
       'max_people' => '5',
       'min_people' => '5',
       'max_bags' => '4',
       'min_bags' => '1',
       'price' => '43.50',
    )),
    9 => 
    stdClass::__set_state(array(
       'description' => 'Compact SUV',
       'codes' => 
      array (
        0 => 'CFAR',
      ),
       'max_people' => '5',
       'min_people' => '5',
       'max_bags' => '3',
       'min_bags' => '3',
       'price' => '46.42',
    )),
    10 => 
    stdClass::__set_state(array(
       'description' => 'Full-Size Hybrid',
       'codes' => 
      array (
        0 => 'FCAH',
      ),
       'max_people' => false,
       'min_people' => false,
       'max_bags' => '',
       'min_bags' => '',
       'price' => '48.00',
    )),
    11 => 
    stdClass::__set_state(array(
       'description' => 'Standard-Size Convertible',
       'codes' => 
      array (
        0 => 'STAR',
      ),
       'max_people' => '4',
       'min_people' => '4',
       'max_bags' => '4',
       'min_bags' => '1',
       'price' => '48.39',
    )),
    12 => 
    stdClass::__set_state(array(
       'description' => 'Sports Car',
       'codes' => 
      array (
        0 => 'XSAR',
        1 => 'SSAR',
      ),
       'max_people' => '5',
       'min_people' => '4',
       'max_bags' => '3',
       'min_bags' => '1',
       'price' => '48.72',
    )),
    13 => 
    stdClass::__set_state(array(
       'description' => 'Premium Car',
       'codes' => 
      array (
        0 => 'PCAR',
      ),
       'max_people' => '5',
       'min_people' => '5',
       'max_bags' => '4',
       'min_bags' => '2',
       'price' => '48.72',
    )),
    14 => 
    stdClass::__set_state(array(
       'description' => 'Luxury Car',
       'codes' => 
      array (
        0 => 'LCAR',
        1 => 'LDAR',
      ),
       'max_people' => '5',
       'min_people' => '5',
       'max_bags' => '5',
       'min_bags' => '1',
       'price' => '52.16',
    )),
    15 => 
    stdClass::__set_state(array(
       'description' => 'Full-Size SUV',
       'codes' => 
      array (
        0 => 'FFAR',
      ),
       'max_people' => '8',
       'min_people' => '7',
       'max_bags' => '4',
       'min_bags' => '2',
       'price' => '52.92',
    )),
    16 => 
    stdClass::__set_state(array(
       'description' => 'Full-Size Van',
       'codes' => 
      array (
        0 => 'FVAR',
      ),
       'max_people' => '15',
       'min_people' => '12',
       'max_bags' => '5',
       'min_bags' => '1',
       'price' => '90.38',
    )),
    17 => 
    stdClass::__set_state(array(
       'description' => 'Compact Convertible',
       'codes' => 
      array (
        0 => 'CTAR',
      ),
       'max_people' => '4',
       'min_people' => '4',
       'max_bags' => '1',
       'min_bags' => '1',
       'price' => '91.17',
    )),
    18 => 
    stdClass::__set_state(array(
       'description' => 'Premium SUV',
       'codes' => 
      array (
        0 => 'PFAR',
        1 => 'UFAR',
      ),
       'max_people' => '7',
       'min_people' => '5',
       'max_bags' => '5',
       'min_bags' => '1',
       'price' => '92.64',
    )),
    19 => 
    stdClass::__set_state(array(
       'description' => 'Specialty Car',
       'codes' => 
      array (
        0 => 'XCAR',
      ),
       'max_people' => '5',
       'min_people' => '5',
       'max_bags' => '3',
       'min_bags' => '3',
       'price' => '94.42',
    )),
    20 => 
    stdClass::__set_state(array(
       'description' => 'Mid-Size Convertible',
       'codes' => 
      array (
        0 => 'ITAR',
      ),
       'max_people' => '4',
       'min_people' => '4',
       'max_bags' => '2',
       'min_bags' => '2',
       'price' => '97.98',
    )),
    21 => 
    stdClass::__set_state(array(
       'description' => 'Luxury SUV',
       'codes' => 
      array (
        0 => 'LFAR',
      ),
       'max_people' => '5',
       'min_people' => '5',
       'max_bags' => '3',
       'min_bags' => '3',
       'price' => '113.66',
    )),
    22 => 
    stdClass::__set_state(array(
       'description' => 'Premium Convertible',
       'codes' => 
      array (
        0 => 'PTAR',
      ),
       'max_people' => '4',
       'min_people' => '4',
       'max_bags' => '1',
       'min_bags' => '1',
       'price' => '121.15',
    )),
    23 => 
    stdClass::__set_state(array(
       'description' => 'Luxury Convertible',
       'codes' => 
      array (
        0 => 'LTAR',
      ),
       'max_people' => '4',
       'min_people' => '4',
       'max_bags' => '1',
       'min_bags' => '1',
       'price' => '129.01',
    )),
    24 => 
    stdClass::__set_state(array(
       'description' => 'Premium Van',
       'codes' => 
      array (
        0 => 'PVAR',
      ),
       'max_people' => '15',
       'min_people' => '15',
       'max_bags' => '1',
       'min_bags' => '1',
       'price' => '159.49',
    )),
    25 => 
    stdClass::__set_state(array(
       'description' => 'Specialty Convertible',
       'codes' => 
      array (
        0 => 'XTAR',
      ),
       'max_people' => '4',
       'min_people' => '4',
       'max_bags' => '2',
       'min_bags' => '2',
       'price' => '307.64',
    )),
  )

代码:

usort($car_types,function($a,$b) {
    return $a->price == $b->price ? 0 : $a->price > $b->price;
});

usort($car_types, function($a, $b) use ($order) {
    $a_set = intval(isset($order[$a->description]));
    $b_set = intval(isset($order[$b->description]));

    return $a_set === $b_set && $a_set === 1
    ? $order[$a->description] - $order[$b->description]
    : $b_set - $a_set;
});

【问题讨论】:

  • sort order 值添加到对象并使用它进行排序。
  • 您需要对这些类型进行排序,然后才能进行排序。或者您必须使用这些名称创建一个预定义的数组,然后根据这些名称对您的数组进行排序。

标签: php arrays sorting multidimensional-array stdclass


【解决方案1】:

定义排序数组并在usort中使用:

$order = [
    'Economy Car' => 1,
    'Compact Car' => 2,
    'Mid-Size Car' => 3,
    'Standard-Size Car' => 4,
    'Full-Size Car' => 5,
    'Premium Car' => 6,
    'Luxury Car' => 7,
    'Standard-Size Convertible' => 8,
    'Mid-Size SUV' => 9,
    'Standard SUV' => 10,
    'Full-Size SUV' => 11,
    'Minivan' => 12
];

usort($car_types, function ($a, $b) use ($order) {
    static $notSetPriority;

    $aPriority = isset($order[$a->description])
        ? $order[$a->description]
        : $notSetPriority ?? ($notSetPriority = count($order) + 1);
    $bPriority = isset($order[$b->description])
        ? $order[$b->description]
        : $notSetPriority;

    if (($comparison = $aPriority - $bPriority ) === 0) {
        return floatval($a->price) - floatval($b->price);
    }

    return $comparison;
});

如果描述的订单重量没有设置,那么我们给它一个最大重量加一的重量。

不要多次申请usort,因为这样很难预测结果。我已将两种排序标准合二为一。

【讨论】:

  • 非常感谢!太棒了。一件事:对于未在 $order 中定义的剩余汽车(例如中型特种车)...如何按价格对剩余汽车进行排序?因此,例如在小型货车之后应该出现中型特种车、紧凑型 SUV、标准尺寸皮卡、特种车等......
  • @MichaelAlterio,首先按价格对数组进行排序(应用您在问题中指定的usort),然后按描述排序(来自答案的usort)。这应该可以解决问题。
  • 我已经用按价格(从最低到最高)排序的输入数组更新了原始帖子。当我按价格排序然后使用上面的订单排序时,它不是在订单之后按最低到最高价格排序。有什么想法吗?
  • @MichaelAlterio,如果您为输入数组提供var_export(而不是var_dumpprint_r),那么我可以复制粘贴它并尝试,我会尝试找出是什么错了。
  • 谢谢!我已经用 var_export 更新了输入数组。告诉我你发现了什么。
猜你喜欢
  • 1970-01-01
  • 2017-04-08
  • 1970-01-01
  • 2010-09-26
  • 2017-02-19
  • 2014-01-01
  • 2017-12-14
  • 1970-01-01
  • 2019-09-24
相关资源
最近更新 更多