Sorting two dimentional array by column?

Paddy paddy3118 at googlemail.com
Wed Jul 2 18:09:39 EDT 2008


On Jul 2, 10:35 pm, Tobiah <t... at tobiah.org> wrote:
> > Imagine an excel spreadsheet.  I can choose
> > a column and sort the records based on the items
> > in that column.  I would like to do the same
> > thing with a large two dimensional array.
> > What would be the fastest way (in computation time)
> > to accomplish this?
>
> Now that I think about the problem more, I really want
> to sort an array of dictionaries according one of the
> keys of each.  Could I use:
>
> array.sort(key = something)
>
> Where something looks at the proper item in the
> current row?  I can't quite visualize how to pull
> the item out of the dictionary.
>
> Thanks
>
> ** Posted fromhttp://www.teranews.com**

Hi Tobiah,
Try this:

arrayofdicts.sort(
  key = lambda dictinarray: dictinarray.get(sortkeyname)
  )

- Paddy.



More information about the Python-list mailing list