0

I'm trying to first sort by quantity in Rank and if the quantity is equal then sort numerically by Rank.

It might sound complicated but here is the pseudo-code I envision

  1. First sort arrays by the number of repeated values in Rank.
  2. If number of repeated values is equal sort numerically by the value in Rank.

I guess its kinda recursive seeing as the 2nd part is performed on all sub-arrays.

I've been trying usort but I can't get it so see the count of repeated rank values in the array. Multisort doesn't seem to fit either.

Example:

Array
(
    [0] => Array
        (
            [Rank] => 7
            [Suit] => Hearts
        )

    [1] => Array
        (
            [Rank] => 3
            [Suit] => Hearts
        )

    [2] => Array
        (
            [Rank] => 6
            [Suit] => Spades
        )

    [3] => Array
        (
            [Rank] => 10
            [Suit] => Spades
        )

    [4] => Array
        (
            [Rank] => 3
            [Suit] => Spades
        )

    [5] => Array
        (
            [Rank] => 6
            [Suit] => Hearts
        )

    [6] => Array
        (
            [Rank] => 2
            [Suit] => Clubs
        )

)

According to my algorithm

Array
(

    [0] => Array
        (
            [Rank] => 6
            [Suit] => Hearts
        )

    [1] => Array
        (
            [Rank] => 6
            [Suit] => Spades
        )

    [2] => Array
        (
            [Rank] => 3
            [Suit] => Spades
        )


    [3] => Array
        (
            [Rank] => 3
            [Suit] => Hearts
        )

    [4] => Array
        (
            [Rank] => 10
            [Suit] => Spades
        )

    [5] => Array
        (
            [Rank] => 7
            [Suit] => Hearts
        )

    [6] => Array
        (
            [Rank] => 2
            [Suit] => Clubs
        )

)
0

2 Answers 2

2

To avoid performing the counting operation multiple times, you should populate the lookup array of ranks and their counts and cache it as a variable ($rankCounts). Using native functions is a concise way of directly generating the lookup array.

Pass the lookup array into the scope of the usort() call with use().

The spaceship operator makes the comparison logic very clean and readable. Write corresponding values into the array on either side of the operator to declare the order of the sorting conditions. Write $b values on the left and $a values on the right to achieve DESC order.

Code: (Demo)

$rankCounts = array_count_values(array_column($array, 'Rank'));

usort(
    $array,
    function($a, $b) use ($rankCounts) {
        return [$rankCounts[$b['Rank']], $b['Rank']] <=> [$rankCounts[$a['Rank']], $a['Rank']];
    }
);

var_export($array);

If you want to add another sorting rule, just add it as element [2] to both arrays inside of usort and you are finished. The spaceship operator is very handy and powerful.

Sign up to request clarification or add additional context in comments.

Comments

1

The easiest solution I could come up with, is to use a second array in which we'll store the card count for each rank. We can then pass this array to the sorting function in order to get the result we want.

Here's an example of how this would look:

$cards = [...];

$ranks = [];

// Count the cards for each rank
foreach ($cards as $card) {
    if (!isset($ranks[$card['Rank']])) {
        $ranks[$card['Rank']] = 0;
    }

    $ranks[$card['Rank']]++;
}

// Sort the cards array
usort($cards, function ($a, $b) use ($ranks) {
    // If the cards count is the same for the rank, compare rank
    if ($ranks[$a['Rank']] == $ranks[$b['Rank']]) {
        return $a['Rank'] - $b['Rank'];
    }

    // Compare the card count for the rank
    return $ranks[$a['Rank']] - $ranks[$b['Rank']];
});

6 Comments

Thank you for the reply, I'll test it in a second but I am unsure as to why you are sorting by suit at the end?
Oh sorry, I assumed you wanted to do that as well. I came up with an easier version, I'll edit my answer in a second.
I think the current version is much more clear for what you're trying to accomplish.
Hmm, it seems to be only organizing by rank.
I forgot to remove the count calls from the previous solution. It should be all right now.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.