Reputation: 11
I have the following code
class Tupla:
def __init__(self, xid, prob):
self.xid = xid
self.prob = prob
and I append to a list some objects of this class
print myList1
>> [('X10', 11, ''), ('X9', 11, ''), ('X11', 11, ''), ('X2', 11, '')]
I try to order it
myList1 = sorted(myList1, key=lambda Tupla: Tupla.xid, reverse=True)
print myList1
>> [('X9', 11, ''), ('X2', 11, ''), ('X11', 11, ''), ('X10', 11, '')]
I am looking for human sorting. I need order the list like this:
[('X2', 11, ''), ('X9', 11, ''), ('X10', 11, ''), ('X11', 11, '')]
How can I do that?
Upvotes: 1
Views: 97
Reputation: 43078
def Tupla(xid, prob):
class _Tupla:
def __init__(self):
pass
def get_id(self):
return xid
def get_prob(self):
return prob
return _Tupla()
myList = map(lambda arg: Tupla(*arg), (('X10', 11), ('X9', 11), ('X11', 11), ('X2', 11)))
myList.sort(key = lambda obj: int(obj.get_id()[1:]))
print [(x.get_id(), x.get_prob()) for x in myList]
Output:
[('X2', 11), ('X9', 11), ('X10', 11), ('X11', 11)]
Upvotes: 1