Custom Sort Some Data

Example of a custom sort in Python

In [17]:
from random import shuffle
In [18]:
sort_order = ('meta', 'meta', 'link', 'script', 'script', 'script', 'title')
In [19]:
data = [
    *[
        {
            "link": {
                "integrity": "sha384-ggOyR0iXCbMQv3Xipma34MD+dH/1fQ784/j6cY/iJTQUOhcWr7x9JvoRxT2MZw1T",
                "href": "https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css",
            }
        },
        {
            "script": {
                "src": "https://code.jquery.com/jquery-3.3.1.slim.min.js",
                "integrity": "sha384-q8i/X+965DzO0rT7abK41JStQIAqVgRVzpbzo5smXKp4YfRvH+8abtTE1Pi6jizo",
            }
        },
        {
            "script": {
                "src": "https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.7/umd/popper.min.js",
                "integrity": "sha384-UO2eT0CpHqdSJQ6hJty5KVphtPhzWj9WO1clHTMGa3JDZwrnQq4sF86dIHNDz0W1",
            }
        },
        {
            "script": {
                "src": "https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/js/bootstrap.min.js",
                "integrity": "sha384-JjSmVgyd0p3pXB1rRibZUAYoIIy6OrQ6VrjIEaFf/nJGzIxFDsf4x0xIM+B07jRM",
            }
        },
    ],
    *[
        {key: value}
        for key, value in (
            zip(
                ["meta", "meta", "title"],
                [
                    {"charset": "utf-8"},
                    {
                        "name": "viewport",
                        "content": "width=device-width, initial-scale=1, shrink-to-fit=no",
                    },
                    {},
                ],
            )
        )
    ],
]
data_ = data.copy()
In [20]:
shuffle(data_)
In [21]:
assert not data == data_
In [22]:
data_
Out[22]:
[{'link': {'integrity': 'sha384-ggOyR0iXCbMQv3Xipma34MD+dH/1fQ784/j6cY/iJTQUOhcWr7x9JvoRxT2MZw1T',
   'href': 'https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css'}},
 {'script': {'src': 'https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.7/umd/popper.min.js',
   'integrity': 'sha384-UO2eT0CpHqdSJQ6hJty5KVphtPhzWj9WO1clHTMGa3JDZwrnQq4sF86dIHNDz0W1'}},
 {'script': {'src': 'https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/js/bootstrap.min.js',
   'integrity': 'sha384-JjSmVgyd0p3pXB1rRibZUAYoIIy6OrQ6VrjIEaFf/nJGzIxFDsf4x0xIM+B07jRM'}},
 {'title': {}},
 {'meta': {'charset': 'utf-8'}},
 {'meta': {'name': 'viewport',
   'content': 'width=device-width, initial-scale=1, shrink-to-fit=no'}},
 {'script': {'src': 'https://code.jquery.com/jquery-3.3.1.slim.min.js',
   'integrity': 'sha384-q8i/X+965DzO0rT7abK41JStQIAqVgRVzpbzo5smXKp4YfRvH+8abtTE1Pi6jizo'}}]
In [32]:
def key(item):
    key_, = item.keys()
    return sort_order.index(key_)
In [33]:
sorted(data_, key=key )
Out[33]:
[{'meta': {'charset': 'utf-8'}},
 {'meta': {'name': 'viewport',
   'content': 'width=device-width, initial-scale=1, shrink-to-fit=no'}},
 {'link': {'integrity': 'sha384-ggOyR0iXCbMQv3Xipma34MD+dH/1fQ784/j6cY/iJTQUOhcWr7x9JvoRxT2MZw1T',
   'href': 'https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css'}},
 {'script': {'src': 'https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.7/umd/popper.min.js',
   'integrity': 'sha384-UO2eT0CpHqdSJQ6hJty5KVphtPhzWj9WO1clHTMGa3JDZwrnQq4sF86dIHNDz0W1'}},
 {'script': {'src': 'https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/js/bootstrap.min.js',
   'integrity': 'sha384-JjSmVgyd0p3pXB1rRibZUAYoIIy6OrQ6VrjIEaFf/nJGzIxFDsf4x0xIM+B07jRM'}},
 {'script': {'src': 'https://code.jquery.com/jquery-3.3.1.slim.min.js',
   'integrity': 'sha384-q8i/X+965DzO0rT7abK41JStQIAqVgRVzpbzo5smXKp4YfRvH+8abtTE1Pi6jizo'}},
 {'title': {}}]
In [34]:
def reverse_key(item):
    key_, = item.keys()
    return -sort_order.index(key_)
In [35]:
sorted(data_, key=reverse_key )
Out[35]:
[{'title': {}},
 {'script': {'src': 'https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.7/umd/popper.min.js',
   'integrity': 'sha384-UO2eT0CpHqdSJQ6hJty5KVphtPhzWj9WO1clHTMGa3JDZwrnQq4sF86dIHNDz0W1'}},
 {'script': {'src': 'https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/js/bootstrap.min.js',
   'integrity': 'sha384-JjSmVgyd0p3pXB1rRibZUAYoIIy6OrQ6VrjIEaFf/nJGzIxFDsf4x0xIM+B07jRM'}},
 {'script': {'src': 'https://code.jquery.com/jquery-3.3.1.slim.min.js',
   'integrity': 'sha384-q8i/X+965DzO0rT7abK41JStQIAqVgRVzpbzo5smXKp4YfRvH+8abtTE1Pi6jizo'}},
 {'link': {'integrity': 'sha384-ggOyR0iXCbMQv3Xipma34MD+dH/1fQ784/j6cY/iJTQUOhcWr7x9JvoRxT2MZw1T',
   'href': 'https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css'}},
 {'meta': {'charset': 'utf-8'}},
 {'meta': {'name': 'viewport',
   'content': 'width=device-width, initial-scale=1, shrink-to-fit=no'}}]