Skip to content

Instantly share code, notes, and snippets.

@arsho
Last active September 3, 2021 00:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save arsho/8617405552538ed915372c0cffeec6c7 to your computer and use it in GitHub Desktop.
Save arsho/8617405552538ed915372c0cffeec6c7 to your computer and use it in GitHub Desktop.
Sort Python Dictionary by key, value or combined. Sort custom class.

Problem

Given an array of integers, sort its elements by the difference of their largest and smallest digits. In the case of a tie, that with the larger index in the array should come first.

Example

For a = [152, 23, 7, 887, 243], the output should be digitDifferenceSort(a) = [7, 887, 23, 243, 152].

Here are the differences of all the numbers:

152: difference = 5 - 1 = 4;
23: difference = 3 - 2 = 1;
7: difference = 7 - 7 = 0;
887: difference = 8 - 7 = 1;
243: difference = 4 - 2 = 2.
23 and 887 have the same difference, but 887 goes after 23 in a, so in the sorted array it comes first.

Solution

class DataClass(object):
    def __init__(self, value, diff, index):
        self.value = value
        self.diff = diff
        self.index = index
    def __repr__(self):
        return "{}-->{}-->{}".format(self.value, self.diff, self.index)

def digitDifferenceSort(a):
    data = []
    for i in range(len(a)):
        current = a[i]
        s = list(map(int,str(current)))
        max_digit = max(s)
        min_digit = min(s)
        diff = abs(max_digit - min_digit)
        element = DataClass(current, diff, i)
        data.append(element) 
    result = [i.value for i in sorted(data, key = lambda x:(x.diff, -x.index))]
    return result

Data

world_cup_wins = {
    "Brazil": 5,
    "Germany": 4,
    "Italy": 4,
    "Argentina": 2,
    "France": 2,
    "Uruguay": 2,
    "England": 1,
    "Spain": 1
}

SORT DICTIONARY BY KEY ASCENDING

for key, value in sorted(world_cup_wins.items(), key=lambda x: x[0]):
    print(key, value)
Argentina 2
Brazil 5
England 1
France 2
Germany 4
Italy 4
Spain 1
Uruguay 2

SORT DICTIONARY BY KEY DESCENDING

for key, value in sorted(world_cup_wins.items(), key=lambda x: x[0], reverse=True):
    print(key, value)
Uruguay 2
Spain 1
Italy 4
Germany 4
France 2
England 1
Brazil 5
Argentina 2

SORT DICTIONARY BY VALUE ASCENDING

for key, value in sorted(world_cup_wins.items(), key=lambda x: x[1]):
    print(key, value)
Spain 1
England 1
Uruguay 2
Argentina 2
France 2
Italy 4
Germany 4
Brazil 5

SORT DICTIONARY BY VALUE DESCENDING

for key, value in sorted(world_cup_wins.items(), key=lambda x: x[1], reverse=True):
    print(key, value)
Brazil 5
Italy 4
Germany 4
Uruguay 2
Argentina 2
France 2
Spain 1
England 1

SORT DICTIONARY BY VALUE ASCENDING AND THEN BY KEY ASCENDING

for key, value in sorted(world_cup_wins.items(), key=lambda x: (x[1], x[0])):
    print(key, value)
England 1
Spain 1
Argentina 2
France 2
Uruguay 2
Germany 4
Italy 4
Brazil 5

SORT DICTIONARY BY VALUE DESCENDING AND THEN BY KEY ASCENDING

for key, value in sorted(world_cup_wins.items(), key=lambda x: (-x[1], x[0])):
    print(key, value)
Brazil 5
Germany 4
Italy 4
Argentina 2
France 2
Uruguay 2
England 1
Spain 1
@catchsrinivas
Copy link

Good one, Probably you can even add about Python 3.7 version where dictionaries have been modified to maintain insertion order. Checkout the reference article on Sort Dictionary by value in Python

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment