Python对List中的元素排序

首先定义一个compare函数:

def compare(sf1, sf2):
    if (sf1.value > sf2.value):
        return -1;
    elif (sf1.value == sf2.value):
        return 0;
    else:
        return 1; 

然后调用该函数就可以对List中的元素排序:

listA.sort(compare)

要求ListA中的元素有value这个属性才行,当然也可以把value换成ListA中的元素的其他共有属性也可以。感觉和Java差不多。

原文地址:https://www.cnblogs.com/mstk/p/8684575.html