how to order list by count of record in each group by section?

samani

Member
hi
i use group by in my list and i want sort descending of group with max record. is it possible and how? for example i want come the group of maximum count of record to first of list.
 
Hi, haven't tried or tested this approach, but I would try something like this:

1) Create a hidden field on that list;
2) Update the field with the number of records in group with php list plugin on every time the list loads;
3) In list settings "group by" section, select to order by that hidden field.
 
@juuser imho i don't think that adding a hidden field in the same list for counting the grouped records of that list will work. Can you explain more pls?
I recall that I was looking also for a solution on this.
 
When you have that extra field, you will have your list something like:

count_and_order_element | your_groupby_element, etc
3 | some_element_name
3 | some_element_name
3 | some_element_name
2 | some_element_name1
2 | some_element_name1
1 | some_element_name2

Now you can order your list by that "count_and_order_element". And one mistake in my first post, the ordering should be set for the whole list in Data->Order by, not in Order by -> Order by field which is for grpup internal ordering.

Just tested with small existing table with "count_and_order_element" values added manually. Seems to be working as it should.
 
So basically what you say is to put the php list plugin onLoad running the query updating the "hidden element" and then return the grouped records ordered based on that alement?
I see that this could work in a joined table case.
 
@dimoss @juuser
i do this and it work for me:
you must make a new layout (copy from bootstrap folder) template and select that in the List>Details>layout>Templates>Front-end template.
then i use this code after line 92 in default.php:

Code:
    $new_rows = $this->rows;
        if($this->isGrouped) {
            $rows = $this->rows;
            function cmp($a,$b) {
                if(count($a) == count($b)) return 0;
                return count($a) < count($b)?1:-1;
            }
            $keys = array_keys($rows);
         
            usort($rows, "cmp");
            $new_rows = [];
            foreach ($rows as $index => $r) {
                $new_rows[$r[0]->data->_groupId] = $r;
            }
        }
        foreach ($new_rows as $groupedBy => $group) :

in this way when you select group by other element , then you can see each element can be select as Group by option and sort group according descendeing.
test it and feedback to me
 
We are in need of some funding.
More details.

Thank you.

Members online

No members online now.
Back
Top