One of the Integer strings answers I like to use a tuple a little better, because it is cleaner and also more general (works for arbitrary elements, not just strings):
sorted(key=lambda x : ((1 if x[:1] in ("S", "s") else 2), x))
Explanation:
The key parameter allows you to sort the array based on f(item) values ββinstead of item values, where f is an arbitration function.
In this case, the function is anonymous (lambda) and returns a tuple where the first element is the "group" in which you want your element to end (for example, 1 if the line starts with "s" and 2 otherwise).
Using a tuple works because comparing tuples is lexicographically by element, and therefore, when sorting, the group code will weigh more than the element.
source share