>
> 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.
 

> 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)

Anthony

-- 
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 [email protected].
For more options, visit https://groups.google.com/d/optout.

Reply via email to