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中的元素排序:spa

listA.sort(compare)

要求ListA中的元素有value這個屬性才行,固然也能夠把value換成ListA中的元素的其餘共有屬性也能夠。感受和Java差很少。code

相關文章
相關標籤/搜索