# Sorting a multidimensional list in python



## bharatk (Jan 27, 2010)

Hello 

I have created a multidimensional list in python

I used the following code:


```
r =[(300, 4), (5, 6), (100, 2)]
```
I tried sorting it in ascending order using r.sort()
and I get [(5, 6), (100, 2), (300, 4)]

I want it to get sorted based on each on the 2nd element instead of the first. That is the result should be [(100,*2*), (300,*4*), (5,*6*)]

How do I go about doing this?

Thank you.


----------



## Stu_computer (Jul 7, 2005)

```
r =[(300, 4), (5, 6), (100, 2)]
r.sort(lambda x, y: cmp(x[1],y[1]))
```


----------



## bharatk (Jan 27, 2010)

Thank you, it worked perfectly. 
I also realized it can be achieved by the itemgetter of the operator library.


```
r=sorted (r, key=operator.itemgetter(1))
```
I have one more question. Starting a separate thread for that.


----------

