XF 2.1 Group and order by count?

asprin

Active member
For a table having data as the following:

user_id
2
4
1
5
6
3
1
6

I'm using the following query and grouping by user_id

PHP:
$d = \XF::finder('Asprin\FB:Foo')->fetch();
$grouped = $d->groupBy('user_id');
\XF::dump($grouped);

This is giving me the following output:
Code:
array:6 [▼
  6 => array:2 [▶]
  5 => array:1 [▶]
  4 => array:1 [▶]
  3 => array:1 [▶]
  2 => array:1 [▶]
  1 => array:2 [▶]
]

This is correct representation of grouping but is there a way I can order them by the array count so that it looks something like this?

Code:
array:6 [▼
  6 => array:2 [▶]
  1 => array:2 [▶]
  4 => array:1 [▶]
  3 => array:1 [▶]
  2 => array:1 [▶]
  5 => array:1 [▶]
]
 
Solution
This is correct representation of grouping but is there a way I can order them by the array count so that it looks something like this?
PHP:
uasort($grouped, function ($a, $b) {
    $countA = count($a);
    $countb = count($b);

    if ($countA === $countB)
    {
        return 0;
    }
   
    return ($countA < $countB) ? -1 : 1;
});
This is correct representation of grouping but is there a way I can order them by the array count so that it looks something like this?
PHP:
uasort($grouped, function ($a, $b) {
    $countA = count($a);
    $countb = count($b);

    if ($countA === $countB)
    {
        return 0;
    }
   
    return ($countA < $countB) ? -1 : 1;
});
 
Solution
Top Bottom