python 排序 由大到小

 

import functools

class Solution:
    # @param {integer[]} nums
    # @return {string}
    def largestNumber(self, nums):

        def comparator(x, y):   # inputs are string representations of non-negative ints
            if x+y > y+x:       # no need to convert to int because x+y and y+x are same length
                return -1       # so lexicographic string sort behaves like numeric sort
            else:
                return 1

        nums = list(map(str, nums))     # convert to strings
        nums.sort(key=functools.cmp_to_key(comparator))

        return nums
ls=[1,6,4,9,2,8]        
x=Solution()
print(x.largestNumber(ls))

输出

['9', '8', '6', '4', '2', '1']
[Program finished]

 

posted @ 2019-03-29 10:05  anobscureretreat  阅读(1508)  评论(0编辑  收藏  举报