Electronics & Programming

develissimo

Open Source electronics development and programming

  • You are not logged in.

#1 June 9, 2010 15:38:26

N.
Registered: 2009-11-02
Reputation: +  0  -
Profile   Send e-mail  

ordering integers with regroup


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 athttp://groups.google.com/group/django-users?hl=en.

Offline

#2 June 9, 2010 16:04:21

Scott G.
Registered: 2009-11-02
Reputation: +  0  -
Profile   Send e-mail  

ordering integers with regroup


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 athttp://groups.google.com/group/django-users?hl=en.

Offline

#3 June 9, 2010 16:34:55

N.
Registered: 2009-11-02
Reputation: +  0  -
Profile   Send e-mail  

ordering integers with regroup


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 athttp://groups.google.com/group/django-users?hl=en.

Offline

#4 June 11, 2010 16:34:14

Euan G.
Registered: 2009-11-02
Reputation: +  0  -
Profile   Send e-mail  

ordering integers with regroup


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)

# Finally return the detokenized data:
return

if __name__ == "__main__":
unordered_data =

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 athttp://groups.google.com/group/django-users?hl=en.

Offline

Board footer

Moderator control

Enjoy the 14th of December
PoweredBy

The Forums are managed by develissimo stuff members, if you find any issues or misplaced content please help us to fix it. Thank you! Tell us via Contact Options
Leave a Message
Welcome to Develissimo Live Support