I’m currently making a simple code that allows the user to input a sentence or a group of words and the program will sort the words alphabetically. This initial code works great but unfortunately, it prints out the uppercase words first and then the lowercase words. I was hoping if there was a simple way to reverse this and have the lowercase words go first on the list instead of the uppercase words. Any help would be greatly appreciated!
JavaScript
x
10
10
1
strings = input("Enter words: ")
2
3
words = strings.split()
4
5
words.sort()
6
7
print("The sorted words are:")
8
for word in words:
9
print(word)
10
Advertisement
Answer
You can pass a custom key function to sort with.
JavaScript
1
2
1
words.sort(key=lambda s: (s[0].isupper(), s))
2