Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to sort a list by the 2nd tuple element in python and C#

Tags:

python

c#

sorting

I had a list of tuples where every tuple consists of two integers and I wanted to sort by the 2nd integer. After looking in the python help I got this:

sorted(myList, key=lambda x: x[1])

which is great. My question is, is there an equally succinct way of doing this in C# (the language I have to work in)? I know the obvious answer involving creating classes and specifying an anonymous delegate for the whole compare step but perhaps there is a linq oriented way as well. Thanks in advance for any suggestions.

like image 448
Damian Kennedy Avatar asked May 26 '10 01:05

Damian Kennedy


1 Answers

Another way to do it in python is this

from operator import itemgetter
sorted(myList, key=itemgetter(1))
like image 150
John La Rooy Avatar answered Oct 22 '22 00:10

John La Rooy