Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to sort a list of strings with a different order?

I am writing a function in Python which can sort my list. The problem is that I don't want it in the same order that the sorted() method used. I tried using the sorting() method, but when I sort this string, which I it comes out like this:

0123456789abcdefghijklmnopqrstuvwxyzßàáäåæçèéêìíîñòóôöøùúüžα

The order I want it to be in is:

0123456789aàáäåæbcçdeèéêfghiìíîjklmnñoòóôöøpqrsßtuùúüvwxyzžα

Now, I've got a list like this (example):

list = ['x', 'h', 'ê', 'ø', '5', 'ž', 'z', 'α', '3', '1']

And I want so sort it. If I'd use the sorted() method, it would look like this:

['1', '3', '5', 'h', 'x', 'z', 'ê', 'ø', 'ž', 'α']

But I want it to be in the same order as the string I gave before.

like image 296
Arthur Martens Avatar asked May 20 '16 20:05

Arthur Martens


People also ask

How do I sort a list in another order?

Use the zip() and sorted() Functions to Sort the List Based on Another List in Python. In this method, we will use the zip() function to create a third object by combining the two given lists, the first which has to be sorted and the second on which the sorting depends.

How would you sort a list of strings?

Use the Python List sort() method to sort a list in place. The sort() method sorts the string elements in alphabetical order and sorts the numeric elements from smallest to largest.

Can you sort a list with different data types?

Python does not guarantee that the sort() function will work if a list contains items of different data types. As long as the items can be compared using the < comparison operator, an attempt will be made to sort the list. Otherwise, an error or exception may be generated.

Can you sort a list of strings Python?

In Python, there are two ways, sort() and sorted() , to sort lists ( list ) in ascending or descending order. If you want to sort strings ( str ) or tuples ( tuple ), use sorted() .


1 Answers

The idea is to associate to each char the index in the specified order and use the indexes of the string chars to do the order comparison.

Note: only works with Python 3

Sort one char strings

ORDER = "0123456789aàáäåæbcçdeèéêfghiìíîjklmnñoòóôöøpqrsßtuùúüvwxyzžα"
# associate each char with the index in the string
# this makes sort faster for multiple invocations when compared with
# ORDER.index(c)
POS = {c:p for (p, c) in enumerate(ORDER)}

lst = ['x', 'h', 'ê', 'ø', '5', 'ž', 'z', 'α', '3', '1']

lst.sort(key = lambda c: POS[c])
# or, suggested by wim
lst.sort(key = POS.get)

Sort any length strings

class MyStrOrder:
    def __init__(self, inner):
        self.inner = inner

    def __lt__(self, other):
        for i in range(min(len(self.inner), len(other.inner))):
            a = POS.get(self.inner[i])
            b = POS.get(other.inner[i])
            if a != b:
                return a < b
        return len(self.inner) < len(other.inner)

lst = ["abc", "ab", "aá"]
lst.sort()
print(lst)

lst = ["abc", "ab", "aá"]
lst.sort(key = MyStrOrder)
print(lst)

Outputs:

['ab', 'abc', 'aá']
['aá', 'ab', 'abc']
like image 91
malbarbo Avatar answered Oct 12 '22 15:10

malbarbo