I have a dict in which the values are lists of Objects.
I need to sort them based on one of the object's attributes.
{key1 : [(list1 of obj),(list2 of obj),(list3 of obj)], key2 : [(list1 of obj),(list2 of obj)]}
I need to sort the list of values by one of the attributes, e.g. "Quantity".
My code structure is:
for objlist in mydict[key]:
sorted(objlist ,key=lambda k: (k.Quantity),reverse=True)
sorted(objlist , key=operator.itemgetter)
s = sorted(s, key = lambda x: (x[1], x[2]))
objlist.sort(key=operator.attrgetter("Quantity"), reverse=False)
objlist.sort(key = lambda x: x.Quantity)
I tried all the above options but nothing worked.
sortedreturns a new list, which you are not capturing. What is the class ofQuantity? It need to be a class with the comparison operators implemented.for objlist in mydict.values()