let's say we are given a list of lowercase strings,
I want a function to return a list with the strings sorted in alphabetical order, except group all the strings that begin with 'x' at the beginning of the list.
['mix', 'xyz', 'apple', 'xanadu', 'aardvark']
CodePudding user response:
This might help you.
words = ['mix', 'xyz', 'apple', 'xanadu', 'aardvark']
custom_sort_order = ['x', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'y', 'z']
words = sorted(words, key=lambda word: [custom_sort_order.index(c) for c in word])
print(words)