It should be easy enough to write your own tag providing you're
expecting your input in the form <word><space><numbers>. I've written
a bit of code that should do this:

import re

LABEL_RE = re.compile(r'^(\w+) (\d+)$')

def order_by_number(unordered_data):
    tokenized_data = []
    for item in unordered_data:
        # tokenize this item:
        match = LABEL_RE.match(item)

        if match is None:
            print "No pattern match found for %s" % item
            continue

        tokenized_item = (match.group(1), int(match.group(2)))
        tokenized_data.append(tokenized_item)

    # Now sort by the numbered tokens:
    tokenized_data.sort(key=lambda i: i[1])

    # Finally return the detokenized data:
    return ['%s %d' % t for t in tokenized_data]

if __name__ == "__main__":
    unordered_data = [
        "District 1",
        "District 10",
        "District 14",
        "District 2",
        "District 20",
        "District 29",
        "District 3",
        "District 30",
    ]

    print order_by_number(unordered_data)

You should be able to put this into a filter pretty easily.

Euan

On Jun 9, 4:34 pm, Nick <nickt...@gmail.com> wrote:
> You are right, I wasn't even thinking about it like that. This is bad
> news as the information is coming across as "District 45" from another
> source and so I don't have just a district number to go off of. State
> government data is always terrible to work with.
>
> On Jun 9, 10:04 am, Scott Gould <zinck...@gmail.com> wrote:
>
> > If you're ordering on "District 1", "District 2", etc. then the number
> > is part of a string and will be sorted alphabetically. I image your
> > only recourse will be to use the numeric field directly, and deal with
> > prepending "District " to it in some other fashion.
>
> > On Jun 9, 10:38 am, Nick <nickt...@gmail.com> wrote:
>
> > > Has anyone come across an ordering issue with the regroup tag whereby
> > > if the field that is being ordered is an integer you get the following
> > > problem:
>
> > > say you have the grouped field "District" and the following groupings
>
> > > District 1
> > > District 2
> > > District 3
> > > District 10
> > > District 14
> > > District 20
> > > District 29
> > > District 30
>
> > > Their ordering will come out:
>
> > > District 1
> > > District 10
> > > District 14
> > > District 2
> > > District 20
> > > District 29
> > > District 3
> > > District 30
>
> > > I can see why this is happening, but how do I tell the regroup tag to
> > > sort as though 1 were 01, etc. Adding a 0 to the front isn't really an
> > > option as their are thousands of records that reach from 1 - 250
>
> > > Thanks in advance.

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-us...@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