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.
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.
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.
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.
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() .
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']
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With