﻿ Sort Dictionary Keys by Value, Then by Key - ITCodar

# Sort Dictionary Keys by Value, Then by Key

## Sorting a dictionary by value then by key

``In [62]: y={100:1, 90:4, 99:3, 92:1, 101:1}In [63]: sorted(y.items(), key=lambda x: (x[1],x[0]), reverse=True)Out[63]: [(90, 4), (99, 3), (101, 1), (100, 1), (92, 1)]``

The `key=lambda x: (x[1],x[0])` tells `sorted` that for each item `x` in `y.items()`, use `(x[1],x[0])` as the proxy value to be sorted. Since `x` is of the form `(key,value)`, `(x[1],x[0])` yields `(value,key)`. This causes `sorted` to sort by `value` first, then by `key` for tie-breakers.

`reverse=True` tells `sorted` to present the result in descending, rather than ascending order.

See this wiki page for a great tutorial on sorting in Python.

PS. I tried using `key=reversed` instead, but `reversed(x)` returns an iterator, which does not compare as needed here.

## Sorting a dictionary by value then key

You need to take advantage of the fact that the values are numbers.

``>>> [v[0] for v in sorted(d.iteritems(), key=lambda(k, v): (-v, k))]['peach', 'banana', 'beetroot', 'almond', 'apple']``

## How do I sort a dictionary by value?

### Python 3.7+ or CPython 3.6

Dicts preserve insertion order in Python 3.7+. Same in CPython 3.6, but it's an implementation detail.

``>>> x = {1: 2, 3: 4, 4: 3, 2: 1, 0: 0}>>> {k: v for k, v in sorted(x.items(), key=lambda item: item[1])}{0: 0, 2: 1, 1: 2, 4: 3, 3: 4}``

or

``>>> dict(sorted(x.items(), key=lambda item: item[1])){0: 0, 2: 1, 1: 2, 4: 3, 3: 4}``

### Older Python

It is not possible to sort a dictionary, only to get a representation of a dictionary that is sorted. Dictionaries are inherently orderless, but other types, such as lists and tuples, are not. So you need an ordered data type to represent sorted values, which will be a list—probably a list of tuples.

For instance,

``import operatorx = {1: 2, 3: 4, 4: 3, 2: 1, 0: 0}sorted_x = sorted(x.items(), key=operator.itemgetter(1))``

`sorted_x` will be a list of tuples sorted by the second element in each tuple. `dict(sorted_x) == x`.

And for those wishing to sort on keys instead of values:

``import operatorx = {1: 2, 3: 4, 4: 3, 2: 1, 0: 0}sorted_x = sorted(x.items(), key=operator.itemgetter(0))``

In Python3 since unpacking is not allowed we can use

``x = {1: 2, 3: 4, 4: 3, 2: 1, 0: 0}sorted_x = sorted(x.items(), key=lambda kv: kv[1])``

If you want the output as a dict, you can use `collections.OrderedDict`:

``import collectionssorted_dict = collections.OrderedDict(sorted_x)``

## python sort dictionary items by value and then key

``MyDict = {0: {'Score': 80.0, 'studentName': 'dan'},          1: {'Score': 92.0, 'studentName': 'rob'},          2: {'Score': 10.0, 'StudentName': 'xyz'}}``

This returns the list of key-value pairs in the dictionary, sorted by value from highest to lowest:

``sorted(MyDict.items(), key=lambda x: x[1], reverse=True)``

For the dictionary sorted by key, use the following:

``sorted(MyDict.items(), reverse=True)``

The return is a list of tuples because dictionaries themselves can't be sorted.

This can be both printed or sent into further computation.

## Sort dictionary keys by value, then by key

Xcode 13.2.1 • Swift 5.5.2

``extension Dictionary where Key: Comparable, Value: Comparable {    var valueKeySorted: [(Key, Value)] {        sorted {            \$0.value != \$1.value ?            \$0.value > \$1.value :            \$0.key < \$1.key        }    }}``

``let dict = ["foo" : 1, "bar" : 1, "baz" : 2, "qux" : 2]let keyValueArray = dict.valueKeySortedprint(keyValueArray)   // "[("baz", 2), ("qux", 2), ("bar", 1), ("foo", 1)]"for (key, value) in keyValueArray {    print(key, value)}``

## How to sort a dictionary by value (DESC) then by key (ASC)?

Something like

``In [1]: d = {'banana': 3, 'orange': 5, 'apple': 5}In [2]: sorted(d.items(), key=lambda x: (-x[1], x[0]))Out[2]: [('apple', 5), ('orange', 5), ('banana', 3)]``

## sort dict by value then if equal by keys

`reverse=true` applies to both elements of the tuple. That why your alphanumeric output is backwards.

To reverse the order on the first element of the tuple but not on the other you could negate the numbers:

``sorted(w.items(),key= lambda a: (-a[1],a[0]))``

If you ever run into problem where you need to sort on multiple tuple elements with varying ASC/DESC order and elements that are not easy to negate like integers you could use the following technique.

Modified `multisort` from https://docs.python.org/3/howto/sorting.html#sort-stability-and-complex-sorts

Since the sorting is stable the following will work:

``def multisort(xs, specs):     for i, reverse in reversed(specs):         xs.sort(key=lambda x: x[i], reverse=reverse)     return xsitems = [('to', 2), ('be', 2), ('or', 1), ('not', 1), ('ae', 2)]multisort(items, [(0, False), (1, True)])``

Output:

``[('ae', 2), ('be', 2), ('to', 2), ('not', 1), ('or', 1)]``

## Sorting Dictionary first by value , then by keys in python 3

``my_dictionary = dict({'ca': 'a', 'cb': 'c', 'n': 'b', 'd': 'z', 'f': 'a'})l=my_dictionary.items() # get a list of (k, v)l.sort(key=lambda x: x[0],reverse=False) # sort by key in ascending orderl.sort(key=lambda x: x[1],reverse=True) # sort by value in descending orderordered_keys=[t[0] for t in l] # get an ordered list of the keys``

This code gives you a sorted list of keys which can you use to access the values in the order you want