如何参数化python排序

时间:2014-08-18 06:27:13

标签: python sorting python-2.7

我在python中有以下代码用于排序

cursor.execute('SELECT Column1, Column2, Column3 from Table1');
Details = cursor.fetchall()

def mixed_order(Details):
    return (Details.Column1,
            Details.Column2,
            Details.Column3
            )

Sorted_Details = sorted(Details,key=mixed_order)

我想通过说

来参数化mixed_order函数
Criteria1 ="Details.Column2"
Criteria2 ="Details.Column1"
Criteria3 ="Details.Column3"

并定义以下内容

def mixed_order(Details):
    return (Criteria1,
            Criteria2,
            Criteria3 
            )

但是,python并没有对我的细节进行排序。我定义Criteria并为其分配列的方式有问题。

1 个答案:

答案 0 :(得分:0)

我想你想要这样的东西:

def mixed_order(Details):
    return (getattr(Details, Criteria1),
            getattr(Details, Criteria2),
            getattr(Details, Criteria3)
            )