python之List排序

sorted()

#coding:utf-8
#sorted Ascending 升序
L = [12,23,43,3,65,34,21,3645]
print(sorted(L))

>>>output
[3, 12, 21, 23, 34, 43, 65, 3645]


#sorted Descending 降序
print(sorted(L,reverse = True))

>>>output
[3645, 65, 43, 34, 23, 21, 12, 3]



sorted 还可用于按指定条目排序。简单来说,比如排序一个二维数组,可指定按照array[x][1]排序
具体请参考:Python sorted() 函数

sort()

参考:Python List sort()方法

原文地址:https://www.cnblogs.com/pualus/p/8042449.html