-1

Hi am wondering how to sort a file by name (alphabetically) then by score (numerically - highest to lowest). For example if a name starts with an A (Anna ect) and the other name started with a B (bella) they will be alphabetically sorted, however if bella got a higher score than Anne then belle would go before Anne when the file is printed out.

Lola
  • 1
  • 1
  • 1
    Hint: by supplying a function to the `key` named parameter of `sort`, you can specify whatever kind of ordering logic you like. – Kevin Feb 09 '15 at 19:15
  • 1
    possible duplicate of [Sorting a Python list by two criteria](http://stackoverflow.com/questions/5212870/sorting-a-python-list-by-two-criteria) – Mr. Polywhirl Feb 09 '15 at 19:20
  • also note that my interpretation of your request is you have your ordering backwards... normally if you have Anna (scored 93), another Anna (scored 89) Bella (97), and you sorted by name and then score, you'd get back Anna 93, Anna 89, Bella 97; you want to sort by score and then by name to get Bella 97, Anna 93, Anna 89 – Foon Feb 09 '15 at 19:22
  • Something along the lines of... `sorted(files, key=lambda file: (file.name, file.score)) ` Also, sounds like your explanation is a contradiction. Sort by name then score or score then name... – Mr. Polywhirl Feb 09 '15 at 19:22

1 Answers1

0

Use following for sort first on name and then number

>>> list = [{'name':'ABC','number':45}]
>>> sorted(list, key = lambda d: (d['name'], d['number']))
NIKHIL RANE
  • 4,012
  • 2
  • 22
  • 45