I have the following data: unsorted data
I want to sort this data by first_pass (reverse), level, cat (reverse), acc1. After sorting it should be in this order: sorted data
Here is my the setup in Python:
import math
from operator import attrgetter
class Problem:
def __init__(self, num, first_pass, cat, acc1):
self.num = num
self.first_pass = first_pass
self.cat = cat
self.acc1 = acc1
def level(self):
return math.ceil((1-self.acc1)*10)
def __repr__(self):
return "<Num:%s 1stPass:%s Cat:%s Acc1:%s Level:%s>" % (self.num, self.first_pass, self.cat, self.acc1, self.level())
p = [
Problem(1,1,2,0.582),
Problem(2,1,2,0.44),
Problem(3,0,2,0.857),
Problem(4,0,2,0.707),
Problem(5,0,2,0.826),
Problem(6,0,1,0.781),
Problem(7,0,1,0.653),
Problem(8,1,2,0.492),
Problem(9,0,2,0.893),
Problem(10,0,1,0.582),
Problem(11,0,2,0.378),
Problem(12,0,2,0.515),
Problem(13,0,0,0.958),
Problem(14,0,1,0.633),
Problem(15,0,1,0.813),
Problem(16,0,2,0.793),
Problem(17,0,2,0.692)
]
# p.sort ??
What is the sort command for this type of sort? Thanks!
sorted
function accepts a key argument which you can pass your function as this arg.sorted(p,key = lambda x: x.attribute)
will sort it byattribute