0

In Python I have a list of objects, and I can sort them by attributes a or b using

obj_list = sorted(obj_list, key=lambda x:x.a)

or

obj_list = sorted(obj_list, key=lambda x:x.b)

The sort selected (a or b or many other options) depends on user input, so I'd like to be able to have a single sorting statement and pass it the sort key – something like this:

def sortfunc(attribute):
    return lambda x:attribute

I know that won't work, but is there a way to do this?

mkrieger1
  • 19,194
  • 5
  • 54
  • 65
Mike Christie
  • 381
  • 1
  • 10

0 Answers0