Python对List中的元素排序的方法

作者:猪哥 2018-04-03

首先定义一个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差不多。

相关文章

精彩推荐