簡體   English   中英

我如何結合這種Laravel系列?

[英]How can I combine this Laravel Collection sort?

這是我目前擁有的:

    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調整訂單。

當前,它按第二個執行的函數排序。 我該如何做才能對兩者都進行排序?

我將使用sort方法而不是sortBy sort方法使用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”,而不是“ [“ Land”)完全匹配“]”和'[[“ Basic”]')。

根據問題的邏輯和注釋的屬性格式,您可能正在尋找以下內容:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM