Privacy Policy
Snippets index

  Sort a list of dictionaries in Python3

System Message: WARNING/2 (<string>, line 1)

Cannot analyze code. Pygments package not found.

.. code:: python

    import functools


    def sort_results(data):
        """
        Sort on "anno", than on "label"
        """

        def mycmp(a, b):
            if a < b:
                return -1
            elif a > b:
                return 1
            return 0

        def compare(x, y):
            if x['anno'] == y['anno']:
                return mycmp(x['label'], y['label'])
            return mycmp(x['anno'], y['anno'])

        data.sort(key=functools.cmp_to_key(compare), reverse=False)