Doubt regarding sorting of a list specific field

vincent wehren vincent at visualtrans.de
Wed Apr 13 02:31:00 EDT 2005


"praba kar" <prabapython at yahoo.co.in> schrieb im Newsbeitrag 
news:mailman.1817.1113371391.1799.python-list at python.org...
| Dear All,
|
|    I have doubt regarding sorting. I have a list
| that list have another list (eg)
|
| list = [[1234,'name1'],[2234,'name2'],[0432,'name3']]

-> Be careful, 0432 is octal notation for 282.
|
| I want to sort only numeric value having array field.
| How I need to do for that.

You may want use the decorate-sort-undecorate idiom.
I othere words, you isolate the index you want to sort by,
sort on the indices, than get rid of the indices again.

Something like:

def sortSeqOfSeqs(seq, idx):
    tmp = sorted([(elem[idx], elem) for elem in seq])
    return [elem[1] for elem in tmp]

seq = [[1234,'name1'],[2234,'name2'],[1432,'name3']]
print sortSeqOfSeqs(seq, 0)
# prints [[1234, 'name1'], [1432, 'name3'], [2234, 'name2']]
# Or to sort by the name index
print sortSeqOfSeqs(seq, 1)
# prints [[1234, 'name1'], [2234, 'name2'], [1432, 'name3']]


Is this what you we're looking for?

--

Vincent Wehren








|
| with regards
| Prabahar
|
|
|
|
|
|
| ________________________________________________________________________
| Yahoo! India Matrimony: Find your life partner online
| Go to: http://yahoo.shaadi.com/india-matrimony 





More information about the Python-list mailing list