pythonsorting

Is lexicographical sorting with highest length first possible in python?


Let's say I have a list

list = ['aa', 'bb', 'aa', 'aaa', 'bbb', 'bbbb', 'cc']

if you do list.sort() you get back ['aa', 'aa', 'aaa', 'bb', 'bbb', 'bbbb', 'cc']

Is there a way in python 3 we can get

['aaa', 'aa', 'aa', 'bbbb', 'bbb', 'bb', 'cc']

So within the same lexicographical group order, pick the one with the bigger length first.

Thanks a ton for your help!


Solution

  • You can redefine what less-than means for the strings with a custom class. Use that class as the key for list.sort or sorted.

    class C:
    
        def __init__(self, val):
            self.val = val
    
        def __lt__(self, other):
            min_len = min((len(self.val), len(other.val)))
            if self.val[:min_len] == other.val[:min_len]:
                return len(self.val) > len(other.val)
            else:
                return self.val < other.val
    
    lst = ['aa', 'bb', 'aa', 'aaa', 'bbb', 'bbbb', 'cc']
    slist = sorted(lst, key=C)
    print(slist)