【问题标题】:How can I combine this Laravel Collection sort?我怎样才能结合这个 Laravel Collection 排序?
【发布时间】:2015-07-14 15:51:23
【问题描述】:

这是我目前拥有的:

    use Illuminate\Support\Collection;

    $order = ['Land', 'Planeswalker', 'Creature', 'Instant', 'Sorcery', 'Enchantment', 'Artifact'];
    $landOrder = ['Basic', ''];

    $deck = $deck->sortBy(function($card) use ($landOrder) {
        foreach ($landOrder as $index => $supertypes) {
            if (str_contains($card->supertypes, $supertypes)) {
                return $index;
            }
        }
    })->values();

    $deck = $deck->sortBy(function($card) use ($order) {
        foreach ($order as $index => $type) {
            if (str_contains($card->types, $type)) {
                return $index;
            }
        }
    })->values();

基本上我想先按 $order 排序,然后根据 $landOrder 调整顺序。

目前它按第二个执行的函数排序。我怎样才能使它对两者都进行排序?

【问题讨论】:

    标签: php sorting laravel collections


    【解决方案1】:

    我会使用sort 方法而不是sortBysort 方法使用 PHP 的 uasort 函数,它可以让您更好地控制排序功能。

    $deck->sort(function($a, $b) use ($order, $landOrder) {
        // get the order indexes for the two items
        $ai = array_search($a->types, $order);
        $bi = array_search($b->types, $order);
    
        // if the two items have the same order, drill down to the land order comparison
        if ($ai === $bi) {
            // get the land order indexes for the two items
            $aii = array_search($a->supertypes, $landOrder);
            $bii = array_search($b->supertypes, $landOrder);
    
            // order based on "natural order" comparison of land order indexes
            return strnatcmp($aii, $bii);
        }
    
        // order based on "natural order" comparison of order indexes
        return strnatcmp($ai, $bi);
    })->values();
    

    编辑

    您需要更新函数以实现从$order$landOrder 数组中获取正确索引所需的任何逻辑。我只是将array_search() 用作一个快速简便的函数,但这假设typessupertypes 属性的值将与数组中的条目完全匹配(即“土地”和“基本”,而不是'["Land"]' 和 '["Basic"]')。

    根据您的问题的逻辑以及您的 cmets 的属性格式,您可能正在寻找类似这样的内容:

    $deck->sort(function($a, $b) use ($order, $landOrder) {
        // get the order indexes for the two items
        $ai = null;
        $bi = null;
        foreach ($order as $index => $type) {
            if (str_contains($a->types, $type)) {
                $ai = $index;
            }
            if (str_contains($b->types, $type)) {
                $bi = $index;
            }
        }
        // if the type is not in the array, assign some type of value that will order like types together, but after all other proper types
        if (is_null($ai)) {
            $ai = count($order).$a->types;
        }
        if (is_null($bi)) {
            $bi = count($order).$b->types;
        }
    
        // if the two items have the same order, drill down to the land order comparison
        if ($ai === $bi) {
            // get the land order indexes for the two items
            $aii = null;
            $bii = null;
            foreach ($landOrder as $index => $supertype) {
                if (str_contains($a->supertypes, $supertype)) {
                    $aii = $index;
                }
                if (str_contains($b->supertypes, $supertype)) {
                    $bii = $index;
                }
            }
            // if the supertype is not in the array, assign some type of value that will order like supertypes together, but after all other proper supertypes
            if (is_null($aii)) {
                $aii = count($landOrder).$a->supertypes;
            }
            if (is_null($bii)) {
                $bii = count($landOrder).$b->supertypes;
            }
    
            // order based on "natural order" comparison of land order indexes
            return strnatcmp($aii, $bii);
        }
    
        // order based on "natural order" comparison of order indexes
        return strnatcmp($ai, $bi);
    })->values();
    

    【讨论】:

    • 我试过你的回答,它似乎在做一些事情,但它不太正确。我得到的是这样的:LandBasic1、LandBasic2、Creature1、Land3、Land4、Creature2、Planeswalker1 等。预期结果是:LandBasic1、LandBasic2、Land3、Land4、Planeswalker1、Creature1、Creature2、剩菜等。
    • @rotaercz 究竟是什么代码让你得到了这些结果?对象typessupertypes 字段是什么样的?
    • 类型只是包含 $order 数组中的值的字符串。超类型也是字符串,可能包含也可能不包含 $landOrder 数组中的值。
    • 这里是示例数据: { "0" : { "types" : "[\"Land\"]", "supertypes" : "", }, "1" : { "types" : "[\"Creature\"]", "supertypes" : "", }, "2" : { "types" : "[\"Land\"]", "supertypes" : "", } }
    猜你喜欢
    • 1970-01-01
    • 2021-07-16
    • 1970-01-01
    • 1970-01-01
    • 2022-10-24
    • 2018-03-26
    • 2022-09-24
    • 2012-09-03
    • 1970-01-01
    相关资源
    最近更新 更多