我如何结合这种Laravel Collection排序?

时间:2015-05-05 04:02:39

标签: php sorting laravel collections

这就是我目前所拥有的:

    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调整订单。

目前,它按第二次执行的功能进行排序。我该怎么做才能对它们进行排序?

1 个答案:

答案 0 :(得分: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'和'基本',而不是'[“土地”]'和'[“基本”]')。

根据您问题的逻辑以及评论中的属性格式,您可能正在寻找以下内容:

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