首先定义一个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差不多。
首先定义一个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差不多。
本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。