Specify the sorting direction for the various columns/

O

Oni

Managed to get a dictionary to sort on multiple columns using a tuple
to set the sort order (see below). However how can I control that
column "date" orders descending and the column "name" orders
ascending.

Thanks

import datetime
import pprint
import operator

faUserFormInput = {'DDPageSortOrder': 'PageAge'}
mypages = ["PageName","PageAge","PageAge"]

gaValidSortOrder = [
{'OrderType': 'PageName', 'SortOrder': ('name')},
{'OrderType': 'PageAge', 'SortOrder': ('date','name')},
{'OrderType': 'PageAuthor', 'SortOrder':
('username','date')}
]

entries = [{'name': 'ZZ2', 'username': 'ZZ3', 'date': datetime.datetime
(2008, 9, 30, 16, 43, 54)},{'name': 'ZZ2', 'username': 'ZZ5','date':
datetime.datetime(2008, 9, 30, 16, 43, 54)},{'name': 'ZZ2',
'username': 'ZZ1', 'date': datetime.datetime(2007, 9, 30, 16, 43, 54)},
{'name': 'AA2', 'username': 'AA2','date': datetime.datetime(2007, 9,
30, 16, 43, 54)}]

sortorderarr = ('name','date')

#if ("DDPageSortOrder" in faUserFormInput):
for item in gaValidSortOrder:
print "1=%s" % (item["OrderType"])
print "2=%s" % (faUserFormInput["DDPageSortOrder"])
if (item["OrderType"] == faUserFormInput["DDPageSortOrder"]):
sortorderarr = item["SortOrder"]
#sortorderarr = '\','.join(sortorder)
print sortorderarr

pp = pprint.PrettyPrinter(depth=2)
pp.pprint(entries)

bob = entries
bob.sort(key=operator.itemgetter(*sortorderarr),reverse=True)
pp.pprint(bob)
 
M

Mark Tolonen

Oni said:
Managed to get a dictionary to sort on multiple columns using a tuple
to set the sort order (see below). However how can I control that
column "date" orders descending and the column "name" orders
ascending.

One way is to sort twice...sort on the secondary key first, then the primary
key. sort will maintain the order of equal entries.

-Mark
 
M

Mike Kazantsev

Managed to get a dictionary to sort on multiple columns using a tuple
to set the sort order (see below). However how can I control that
column "date" orders descending and the column "name" orders
ascending. ....
bob = entries
bob.sort(key=operator.itemgetter(*sortorderarr),reverse=True)
pp.pprint(bob)

Note that this accomplishes nothing, since bob and entries are the same
object, so entries.sort and bob.sort are the same method of the same
object.
You can use "copy" module to clone list and it's contents (dict objects)
or just use list constructor to clone list structure only, leaving
contents essencially the same, but in different order.
Or, in this case, you can just use "sorted" function which constructs
sorted list from any iterable.


As for the question, in addition to Mark's suggestion of doing
sub-sorting, you can also construct complex index (code below).
Dunno which would be more efficient in the particular case...

import datetime
import pprint

entries = [{'name': 'ZZ2', 'username': 'ZZ3', 'date':
datetime.datetime (2008, 9, 30, 16, 43, 54)},{'name': 'ZZ2',
'username': 'ZZ5','date': datetime.datetime(2008, 9, 30, 16, 43,
54)},{'name': 'ZZ2', 'username': 'ZZ1', 'date':
datetime.datetime(2007, 9, 30, 16, 43, 54)}, {'name': 'AA2',
'username': 'AA2','date': datetime.datetime(2007, 9, 30, 16, 43,
54)}]

entries.sort(lambda x: (x['name'], -time.mktime(x['date'].timetuple())))

Here time is inversed, yielding reverse sort order by that column.

--
Mike Kazantsev // fraggod.net

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v2.0.11 (GNU/Linux)

iEYEARECAAYFAkox6O4ACgkQASbOZpzyXnGTQwCgmw2sQK+HDM11lpoouo3cA+Sn
JJ8Ani0ijLEDJNv2/Scf7KuTbqOUKmGQ
=gFJp
-----END PGP SIGNATURE-----
 
O

Oni

Thanks for the answers. My goal was to try to avoid hard coding
and add a little shine to the code I have inherited. But its
too far gone already and time is short. So have used Mike's answer.

Mike answer with minor changes:

import datetime
import pprint
import operator
import time

entries = [{'name': 'ZZ2', 'username': 'ZZ3', 'date':
datetime.datetime (2007, 9, 30, 16, 43, 54)},{'name': 'ZZ2',
'username': 'ZZ5','date': datetime.datetime(2008, 9, 30, 16, 43,
54)},{'name': 'ZZ3', 'username': 'ZZ1', 'date':
datetime.datetime(2007, 9, 30, 16, 43, 54)}, {'name': 'AA2',
'username': 'AA2','date': datetime.datetime(2007, 9, 30, 16, 43,
54)}]

entries.sort(key = lambda x: (x['name'], -time.mktime(x
['date'].timetuple()) ))

pp = pprint.PrettyPrinter(depth=2)
pp.pprint(entries)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,289
Messages
2,571,442
Members
48,125
Latest member
jeremysheoran

Latest Threads

Top