On Wed, 2009-01-14 at 21:14 -0800, Delta20 wrote:
> Is there a way to make a model manager get_query_set function return
> items sorted in natural alphanumeric order? order_by returns things in
> rather than the ASCII order, which is not generally what users expect.

Depends very much on which users you're talking about, and since the set
of "natural expectations" of all users contains contradictory elements
(different users expect opposite things), it's not really a good
measure.

In any case, Django's order_by() is a proxy for database sorting, which
is typically lexicographic using ASCII (or unicode, depending on the
setup of the database).

> For example, sorting by name for a set of objects with names: img10,
> img1, img103, img2, img3a, img3b

You've left out a few words from your sentences, so it's unclear if this
is what you're seeing or what you're hoping to see.  If I insert those
values into a table and order by that field, I get back the results in
this order:

         img1
         img10
         img103
         img2
         img3a
         img3b

That isn't going to be amazingly unclear to anybody who's ever used a
computer, since that's the way filenames are sorted pretty much
everywhere. Sure, it might be nice to say you want it to read img1,
img2, img3, etc, but the current output isn't vastly inferior. Also,
this "alphanumeric" sorting that you're after (and it's not really a
good name which is why I've quote it; the current sorting is a logical
sort on alphanumeric values as well) requires some "smarts" from the
sorter to know which digits are actually numbers and which are simple
characters as part of the name and you still end up having to sort
digits relative to letters (for example, think about sorting "a23b" and
"abc". Or, where do "img03" or "imgxyz" sort in the above list? How
about "img1.2"?).

To do things differently at the database level would require passing the
output column names through functions (at the database level). Right
now, there's no way to do that via Django's ORM (and no really concrete
plans to add anything like that, either). Even doing it at the raw SQL
level, unless your database contains some special function to handle
this, would be quite fiddly. The type of sorting function you're after
is very uncommon, since it requires a lot of extra processing for each
element.

Sorry that that sounds like a bunch of bad news, but that's the way
things are. Humans are very adjustable. The lexicographic sorting that
happens now tends to be fairly readable and not particularly confusing.
The alternative is computationally intensive.

Regards,
Malcolm



--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-users@googlegroups.com
To unsubscribe from this group, send email to 
django-users+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to