Mailing List Archive

sort of multiple dictonaries
Hi there,

perhaps a typical newbie-question:

I've got a list of dictonaries like that:

mydics=[.{'sortit': 'no412', 'mode': 'nothing'},
{'sortit': 'no112', 'mode': 'something'},
{'sortit': 'no02', 'mode': 'something else'}]

Is there an easy way to get that list sorted like that:

def sortDictonary(aDictonary, theSortKey="sortit"):
....

Result have to be:

mydics=[.{'sortit': 'no02', 'mode': 'something else'},
{'sortit': 'no112', 'mode': 'something'},
{'sortit': 'no412', 'mode': 'nothing'}]

Any hints?

Ciao,
Holger
sort of multiple dictonaries [ In reply to ]
Holger Jannsen <holger@phoenix-edv.netzservice.de> writes:
> Hi there,
>
> perhaps a typical newbie-question:
>
> I've got a list of dictonaries like that:
>
> mydics=[.{'sortit': 'no412', 'mode': 'nothing'},
> {'sortit': 'no112', 'mode': 'something'},
> {'sortit': 'no02', 'mode': 'something else'}]
>
> Is there an easy way to get that list sorted like that:
>
> def sortDictonary(aDictonary, theSortKey="sortit"):
> ....
>
> Result have to be:
>
> mydics=[.{'sortit': 'no02', 'mode': 'something else'},
> {'sortit': 'no112', 'mode': 'something'},
> {'sortit': 'no412', 'mode': 'nothing'}]
>
> Any hints?

Well, it's actually a list you're sorting isn't it?

mydics.sort(lambda x,y:cmp(x['sortit'],y['sortit']))

should work, if I understand the problem.

HTH
Michael

> Ciao,
> Holger
sort of multiple dictonaries [ In reply to ]
Thank ya' Michael,

Ciao,
Holger

Michael Hudson schrieb:
>
> Holger Jannsen <holger@phoenix-edv.netzservice.de> writes:
> > Hi there,
> >
> > perhaps a typical newbie-question:
> >
> > I've got a list of dictonaries like that:
> >
> > mydics=[.{'sortit': 'no412', 'mode': 'nothing'},
> > {'sortit': 'no112', 'mode': 'something'},
> > {'sortit': 'no02', 'mode': 'something else'}]
> >
> > Is there an easy way to get that list sorted like that:
> >
> > def sortDictonary(aDictonary, theSortKey="sortit"):
> > ....
> >
> > Result have to be:
> >
> > mydics=[.{'sortit': 'no02', 'mode': 'something else'},
> > {'sortit': 'no112', 'mode': 'something'},
> > {'sortit': 'no412', 'mode': 'nothing'}]
> >
> > Any hints?
>
> Well, it's actually a list you're sorting isn't it?
>
> mydics.sort(lambda x,y:cmp(x['sortit'],y['sortit']))
>
> should work, if I understand the problem.
>
> HTH
> Michael