4321A < 0123B,
2345D < 5432D
db(query1).select().sort(mycmp),
orderby='<random>'
is not supported on Google NoSQL. However, in this situation and likewise in many others where built-ins are insufficient, imports can be used:1 2 | import random rows=db(...).select().sort(lambda row: random.random()) |
I have a different application, where I want to order the rows according to a string field, where the string field has a prefix and a suffix, and the suffix is the dominant part of the ordering:
4321A < 0123B,
2345D < 5432D
rows = db(query).select(..., orderby='substr(mytable.myfield, -1), mytable.myfield')
It's really easy to write a custom cmp() for this, and then to do sorted(rows, mycmp), but the result is a list, not a Rows object; in particular, it loses the colnames. If I do
db(query1).select().sort(mycmp),that fails because mycmp() requires 2 arguments, but 1 given.
Or if I use sorted(), how do I turn list back into a Rows object? (Okay, there's a work-around for this ... write the view to handle a list, rather than having a default view, but one of these days I'm bound to need a Rows object.)
rows.records = sorted(rows, mycmp)
I have a different application, where I want to order the rows according to a string field, where the string field has a prefix and a suffix, and the suffix is the dominant part of the ordering:
4321A < 0123B,
2345D < 5432DThe simplest and most efficient method is probably to let the database do the sorting. In SQLite, you can use the substr() function:
rows = db(query).select(..., orderby='substr(mytable.myfield, -1), mytable.myfield')substr(mytable.myfield, -1) selects the last character of myfield (use -2 for the last 2 characters, etc.). Adding mytable.myfield to the orderby breaks any ties by ordering based on the beginning characters of the field.
It's really easy to write a custom cmp() for this, and then to do sorted(rows, mycmp), but the result is a list, not a Rows object; in particular, it loses the colnames. If I do
db(query1).select().sort(mycmp),that fails because mycmp() requires 2 arguments, but 1 given.The "key" function passed to the Python sorted() function should take only a single argument -- an element from the iterator being sorted. If your mycmp requires two arguments, then it should fail with sorted() as well as with Rows.sort(). Just rewrite mycmp so it takes only one argument (a Row object).
Or if I use sorted(), how do I turn list back into a Rows object? (Okay, there's a work-around for this ... write the view to handle a list, rather than having a default view, but one of these days I'm bound to need a Rows object.)Try:
rows.records = sorted(rows, mycmp)Though, as noted above, rows.sort(mycmp) should work just as well (it uses sorted() internally).Anthony
The "key" function passed to the Python sorted() function should take only a single argument -- an element from the iterator being sorted. If your mycmp requires two arguments, then it should fail with sorted() as well as with Rows.sort(). Just rewrite mycmp so it takes only one argument (a Row object).
Ah, the "key" argument. That's what's going on. sorted() has both a cmp and key argument, and I viewed this as a cmp problem.