0

I have a dict that looks like this:

{0: 2, 1: 4, 2: 2, 3: 2, 4: 5}

That is, both the keys and values integers.

I need to sort this dict in this way:

  • By the value first, ascending;
  • by the key, if the values ties, descending.

All I know is that python's sorted() function supports a parameter called "key" but it seems that it just allows to have either key or value at once.

What can I do to achieve?

FYI, the result should be like:

{2: 2, 0: 2, 3: 2, 1: 4, 4: 5}
Memphis Meng
  • 1,267
  • 2
  • 13
  • 34

3 Answers3

2

There's not an easy way to say "this value, ascending, then this one, descending". However, if you negate each of a list of integers, then sort it, then that's the same as sorting it in reverse.

This defines a sorting key which is a tuple:

  • The first value is each dict item's value.
  • The second value is each dict item's key, but negated.
d = {0: 2, 2: 2, 3: 2, 1: 4, 4: 5}


def sort_key(item):
    key, value = item
    return value, -key


print(sorted(d.items(), key=sort_key))

This outputs:

[(3, 2), (2, 2), (0, 2), (1, 4), (4, 5)]

See? The items are grouped by value, and in the event of a tie, by key in descending order.

Kirk Strauser
  • 30,189
  • 5
  • 49
  • 65
1

Dictionaries can't really be sorted, but since 3.6 they preserve insertion ordering, so you can create a new dictionary from the sorted tuple items of the previous dictionary.

To get what you want, you have to do this twice - once by key, then by value. This works because python's sorted is guaranteed to be "stable" - if two items are identical then it won't change them, so the second value sort will preserve the initial key sort if two values match.

input_dictionary = {0: 2, 1: 4, 2: 2, 3: 2, 4: 5}
sorted_by_key = dict(sorted(input_dictionary.items(), key=lambda x: x[0], reverse=True))
sorted_by_both = dict(sorted(sorted_by_key.items(), key=lambda x: x[1]))
print(sorted_by_both)

Demo

Luke Storry
  • 6,032
  • 1
  • 9
  • 22
1

How do I sort a dictionary by value?

data = {0: 2, 1: 4, 2: 2, 3: 2, 4: 5}
sorted_data = dict(sorted(data.items(), key=lambda item:item[1]))
print(sorted_data)

output

{0: 2, 2: 2, 3: 2, 1: 4, 4: 5}
Rima
  • 1,447
  • 1
  • 6
  • 12