[FRPythoneers] sorting a list of dictionaries

Jack Diederich jack at performancedrivers.com
Mon Jan 20 22:16:37 MST 2003


On Mon, Jan 20, 2003 at 07:18:18PM -0700, Andrew Diederich wrote:
> On Mon, 20 Jan 2003, Cary Miller wrote:
> 
> > Could you post an example of one of these data structures?
> 
> Absolutely.  The first one has the dn at the front, the second does not.
> That should be the only difference.  Both are just my entry.  
> 
> Looks like I was smoking crack earlier -- the default result set is a list
> of tuples.  The first in the tuple is a string (the dn) and the second is
> the dictionary.
> 
> straight:
> 
> [('cn=andrew,cn=Recipients,ou=ND,o=NETdelivery', {'rdn': ['andrew'],
> 'sn': ['Diederich'], 'givenName': ['Andrew'],
<snip>> 
> ['Manager, Customer Support'], 'cn': ['Andrew Diederich']})]
> 

If 'exchangelist' is a list of tuples, the following will sort it with
'sn' as primary, and 'givenName' as secondary.

def mysort(a, b):
  return cmp(a[1]['sn'], b[1]['sn']) or cmp(a[1]['givenName'], b[1]['givenName'])

exchangelist.sort(mysort)


-jackdied



More information about the FRPythoneers mailing list