Sorting on a count field in web2py -
i have following code in web2py. trying retrieve how many types of items have in table , count of how many of each of them there are.
count = db.table.field1.count() rows=db((some criteria).select(db.table.field2, count, groupby=db.table.field2) print rows the print of is:
table.field2, count(table.field1) 4,3 6,4 9,2 now sort high low count field, outcome be:
6,4 4,3 9,2 what's best way it? rows=rows.sort(lambda row: row.count(table.field1)) did not work me.
instead of row.count(table.field1), use row['count(table.field1)'] or row[count] (see here).
note, can have database sorting using orderby argument:
rows = db(query).select(db.table.field2, count, groupby=db.table.field2, orderby=count) and descending order: orderby=~count
Comments
Post a Comment