On Wednesday, March 21, 2018 at 11:21:28 AM UTC-7, Dave S wrote: > > > > On Wednesday, March 21, 2018 at 5:30:08 AM UTC-7, Anthony wrote: >> >> 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 >>> >>> >> The 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. >> > > That looks pretty straightforward, once I realize the substr() function is > available =8-0 > > >> >> >>> 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). >> > > 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. > > >> >> >>> 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 >> > > Thanks, it sounds like I was focusing on the wrong area. I guess I should > have peeked in at rows.sort() to see how it worked. As always, you're on > top of the game. > > Finally got to try this out, and of course tried both the substr() and the records = versions. SQLite3's substr() uses the 3rd argument differently than Python does (length, rather than endpoint), but once I got that sorted out everything was good!
Thanks again. /dps "yes, the example was simplified ... omitted the penultimate character" -- Resources: - http://web2py.com - http://web2py.com/book (Documentation) - http://github.com/web2py/web2py (Source code) - https://code.google.com/p/web2py/issues/list (Report Issues) --- You received this message because you are subscribed to the Google Groups "web2py-users" group. To unsubscribe from this group and stop receiving emails from it, send an email to web2py+unsubscr...@googlegroups.com. For more options, visit https://groups.google.com/d/optout.