Doubt regarding sorting of a list specific field
F. Petitjean
littlejohn.75 at news.free.fr
Wed Apr 13 03:20:52 EDT 2005
Le Wed, 13 Apr 2005 01:13:40 -0600, Steven Bethard a écrit :
> praba kar wrote:
>> list = [[1234,'name1'],[2234,'name2'],[0432,'name3']]
>>
>> I want to sort only numeric value having array field.
>> How I need to do for that.
>
> In Python 2.4:
>
> py> import operator
> py> seq = [(1234,'name1'),(2234,'name2'),(1432,'name3')]
> py> seq.sort(key=operator.itemgetter(0))
> py> seq
> [(1234, 'name1'), (1432, 'name3'), (2234, 'name2')]
>
> Note that I've changed your name 'list' to 'seq' since 'list' is
> shadowing the builtin list function in your code, and I've changed your
> nested lists to tuples because they look like groups of differently
> typed objects, not sequences of similarly typed objects. See:
>
> STeVe
nice explaination. But, if the items of the list are tuples the default
rule for comparing tuples is such that
seq.sort()
would be sufficient here.
More information about the Python-list
mailing list