0

In app engine I retrieve a list of items stored in memcache:

items = memcache.get("ITEMS")

and sort them by amount and price:

items.sort(key = lambda x:(x.price, x.amount))

Which works most of the time, when the amount is three digits. However, when I have 2 items with 150 and 1000 amounts for the same price, the entry with 1000 goes before other one. How can I fix this?

mango
  • 1,183
  • 6
  • 17
  • 33
  • Asked and answered: Question: http://stackoverflow.com/questions/3426108/numeric-sort-in-python Answer : http://stackoverflow.com/a/3426155/2225787 – Agi Hammerthief Feb 22 '14 at 21:41

1 Answers1

0

fixed it:

items.sort(key = lambda x:((float)(x.price), (int)(x.amount)))
mango
  • 1,183
  • 6
  • 17
  • 33