# Python – 如何按升序对数值列表进行sorting

``templist = ['25', '50', '100', '150', '200', '250', '300'].` `

` `['25', '50', '100', '150', '200', '250', '300', '33'].` `

` `['150', '200', '25', '250', '300', '33', '50']` `

` `['25', '33', '50', '100', '150', '200', '250', '300']` `

` `SELECT temperature FROM temperatures ORDER BY temperature ASC; -- ascending order SELECT temperature FROM temperatures ORDER BY temperature DESC; -- descending order` `

` `templist = [25, 50, 100, 150, 200, 250, 300, 33] sorted(templist, key=int) # ascending order > [25, 33, 50, 100, 150, 200, 250, 300] sorted(templist, key=int, reverse=True) # descending order > [300, 250, 200, 150, 100, 50, 33, 25]` `

` `>>> sorted([10,3,2]) [2, 3, 10]` `

` `>>> sorted(['10','3','2']) ['10', '2', '3']` `

（因为string顺序是从第一个字符开始的，“1”在“2”之前，不pipe后面跟着什么字符）可以用`key=int`

` `>>> sorted(['10','3','2'], key=int) ['2', '3', '10']` `

` `>>> l = ['10','3','2'] >>> l.sort(key=int) >>> l ['2', '3', '10']` `

` `select temperature from temperatures order by temperature;` `