3

I'm trying to sort this list first by name in reverse. Then, after the first set of results (where flag = "ZAR"), I want to sort the remaining elements by time not in reverse:

import operator
list = [
    {"flag":"ZAR", "time":"17:45"},
    {"flag":"AUS", "time":"17:30"},
    {"flag":"AUS", "time":"17:15"},
    {"flag":"USA", "time":"17:00"},
    {"flag":"GBP", "time":"16:55"},
    {"flag":"ZAR", "time":"16:45"},
    {"flag":"USA", "time":"16:35"},
    {"flag":"GBP", "time":"16:25"},
    {"flag":"ZAR", "time":"16:15"},
]

list.sort(key=operator.itemgetter("time"))
list.sort(key=operator.itemgetter("flag"),reverse=True)


print list

[{'flag': 'ZAR', 'time': '16:15'}, {'flag': 'ZAR', 'time': '16:45'}, {'flag': 'ZAR', 'time': '17:45'}, {'flag': 'USA', 'time': '16:35'}, {'flag': 'USA', 'time': '17:00'}, {'flag': 'GBP', 'time': '16:25'}, {'flag': 'GBP', 'time': '16:55'}, {'flag': 'AUS', 'time': '17:15'}, {'flag': 'AUS', 'time': '17:30'}]

As you can see the flag sort was done correctly, but the other elements are only sorted by time within the sorted country.

What I want is to always have the ZAR first and after that it does not matter what the flag name is, only the time. So it should give this result:

[{'flag': 'ZAR', 'time': '16:15'}, {'flag': 'ZAR', 'time': '16:45'}, {'flag': 'ZAR', 'time': '17:45'}, {'flag': 'GBP', 'time': '16:25'}, {'flag': 'USA', 'time': '16:35'}, {'flag': 'GBP', 'time': '16:55'}, {'flag': 'USA', 'time': '17:00'}, {'flag': 'AUS', 'time': '17:15'}, {'flag': 'AUS', 'time': '17:30'}]

How would this be done?

2
  • 5
    The worst possible name for a list is list. Do not shadow built-ins. Commented Sep 2, 2013 at 11:21
  • You need to clarify your requirement - you're not "sorting a list by two values" in the normal sense of the word. Can you confirm: you want all the ZAR values first (sorted by time or not?) and then you want the remainder of the elements sorted by time regardless of flag value - is that right? Commented Sep 2, 2013 at 11:27

1 Answer 1

5

Amend your code to:

import operator
data = [
    {"flag":"ZAR", "time":"17:45"},
    {"flag":"AUS", "time":"17:15"},
    {"flag":"USA", "time":"17:00"},
    {"flag":"GBP", "time":"16:55"},
    {"flag":"ZAR", "time":"16:45"},
    {"flag":"USA", "time":"16:35"},
    {"flag":"GBP", "time":"16:25"},
    {"flag":"ZAR", "time":"16:15"},
]

data.sort(key=operator.itemgetter("time"))
data.sort(key=lambda L: L['flag'] == 'ZAR', reverse=True)

# [{'flag': 'ZAR', 'time': '16:15'}, {'flag': 'ZAR', 'time': '16:45'}, {'flag': 'ZAR', 'time': '17:45'}, {'flag': 'GBP', 'time': '16:25'}, {'flag': 'USA', 'time': '16:35'}, {'flag': 'GBP', 'time': '16:55'}, {'flag': 'USA', 'time': '17:00'}, {'flag': 'AUS', 'time': '17:15'}, {'flag': 'AUS', 'time': '17:30'}]
Sign up to request clarification or add additional context in comments.

1 Comment

Ah there we go! Makes much more sense! Thanks

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.