首页 > 代码库 > 二维数组的排序

二维数组的排序

 1 result =[[7250,1],[7510,2],[7759,0],[6523,33]] 2  3 copy_result = [] 4  5 for item in result: 6     copy_result.append([int(item[0]),item[1]]) 7  8 print result 9 # output:10 # [[‘7250‘, ‘1‘], [‘7510‘, ‘2‘], [‘7759‘, ‘0‘], [‘6523‘, ‘33‘]]11 12 print copy_result13 # output14 # [[7250, ‘1‘], [7510, ‘2‘], [7759, ‘0‘], [6523, ‘33‘]]15 16 copy_result.sort()17 18 print copy_result19 # output20 # [[6523, ‘33‘], [7250, ‘1‘], [7510, ‘2‘], [7759, ‘0‘]]21 22 back_result = []23 24 for item in copy_result:25     back_result.append([str(item[0]),item[1]])26 27 print back_result28 # output29 # [[‘6523‘, ‘33‘], [‘7250‘, ‘1‘], [‘7510‘, ‘2‘], [‘7759‘, ‘0‘]]
 
用int()和str()转换数据类型

二维数组的排序 sort()方法根据第一维的排序 
 

如果是逆序,用reverse()