-1

I have a certain list and I need to sort it.

[{'inventory_id': 25,
  'location_id': 12,
  'package_id': False,
  'partner_id': False,
  'prod_lot_id': False,
  'product_id': 7,
  'product_qty': 0,
  'product_uom_id': 1,
  'sequence': '10',
  'theoretical_qty': 10.0},
 {'inventory_id': 25,
  'location_id': 12,
  'package_id': False,
  'partner_id': False,
  'prod_lot_id': False,
  'product_id': 8,
  'product_qty': 0,
  'product_uom_id': 1,
  'sequence': '3',
  'theoretical_qty': 15.0},
 {'inventory_id': 27,
  'location_id': 12,
  'package_id': False,
  'partner_id': False,
  'prod_lot_id': False,
  'product_id': 9,
  'product_qty': 0,
  'product_uom_id': 1,
  'sequence': '7',
  'theoretical_qty': 21.0}]

I need to sort these 3 records on the basis of 'sequence' key field.

After the sorting the list output should be:

[{'inventory_id': 25,
  'location_id': 12,
  'package_id': False,
  'partner_id': False,
  'prod_lot_id': False,
  'product_id': 8,
  'product_qty': 0,
  'product_uom_id': 1,
  'sequence': '3',
  'theoretical_qty': 15.0},
 {'inventory_id': 27,
  'location_id': 12,
  'package_id': False,
  'partner_id': False,
  'prod_lot_id': False,
  'product_id': 9,
  'product_qty': 0,
  'product_uom_id': 1,
  'sequence': '7',
  'theoretical_qty': 21.0},
 {'inventory_id': 25,
  'location_id': 12,
  'package_id': False,
  'partner_id': False,
  'prod_lot_id': False,
  'product_id': 7,
  'product_qty': 0,
  'product_uom_id': 1,
  'sequence': '10',
  'theoretical_qty': 10.0}]

The no of records can be variable and will be dynamic Right now they are three records but they can be more than 10 as well.

awesoon
  • 32,469
  • 11
  • 74
  • 99
user32876
  • 65
  • 2
  • 12
  • Note that your desired output isn't exactly sorting on the `'sequence'` value, but rather on its integer representation. – TigerhawkT3 Jan 23 '16 at 07:30
  • Possible duplicate of [How do I sort a list of dictionaries by values of the dictionary in Python?](http://stackoverflow.com/questions/72899/how-do-i-sort-a-list-of-dictionaries-by-values-of-the-dictionary-in-python) – TessellatingHeckler Jan 23 '16 at 07:49

1 Answers1

3

Use list.sort() and specify a key function which will return the value of the sequence key from each dictionary. Here the key function is provided as a lambda, but it could be a function defined with def. The sequence looks like it is numeric, so it should probably be sorted numerically, hence the int() conversion. One final thing, if a dictionary is missing the sequence key the sequence is treated as 0:

vals = [{'theoretical_qty': 10.0, 'product_id': 7, 'sequence': u'10', 'location_id': 12, 'prod_lot_id': False, 'inventory_id': 25, 'package_id': False, 'product_qty': 0, 'product_uom_id': 1, 'partner_id': False}, {'theoretical_qty': 15.0, 'product_id': 8, 'sequence': u'3', 'location_id': 12, 'prod_lot_id': False, 'inventory_id': 25, 'package_id': False, 'product_qty': 0, 'product_uom_id': 1, 'partner_id': False}, {'theoretical_qty': 21.0, 'product_id': 9, 'sequence': u'7', 'location_id': 12, 'prod_lot_id': False, 'inventory_id': 27, 'package_id': False, 'product_qty': 0, 'product_uom_id': 1, 'partner_id': False}]

vals.sort(key=lambda d: int(d.get('sequence', 0)))

from pprint import pprint
pprint(vals)

Output:

[{'inventory_id': 25,
  'location_id': 12,
  'package_id': False,
  'partner_id': False,
  'prod_lot_id': False,
  'product_id': 8,
  'product_qty': 0,
  'product_uom_id': 1,
  'sequence': u'3',
  'theoretical_qty': 15.0},
 {'inventory_id': 27,
  'location_id': 12,
  'package_id': False,
  'partner_id': False,
  'prod_lot_id': False,
  'product_id': 9,
  'product_qty': 0,
  'product_uom_id': 1,
  'sequence': u'7',
  'theoretical_qty': 21.0},
 {'inventory_id': 25,
  'location_id': 12,
  'package_id': False,
  'partner_id': False,
  'prod_lot_id': False,
  'product_id': 7,
  'product_qty': 0,
  'product_uom_id': 1,
  'sequence': u'10',
  'theoretical_qty': 10.0}]
mhawke
  • 84,695
  • 9
  • 117
  • 138