2

Say I have list called sort_me and want to sort with a non lexicographically ordered alphabet called special_alphabet

sort_me = ['appa', 'apple', 'orange', 'carrot']
special_alphabet = "dklmnoabctuvwxyzfghipqrsej"

result = sorted(sort_me, key=?)
print (result)

expected result: ['orange', 'apple', 'appa', 'carrot']

1 Answers1

5

Here's what you can do

sorted(sort_me, key=lambda word: [special_alphabet.index(char) for char in word])

Output

['orange', 'apple', 'appa', 'carrot']

You might want to revisit the expected output posted, apple would precede appa, as l is before a in your special_alphabet

Vishakha Lall
  • 1,178
  • 12
  • 33