Created
September 24, 2021 06:05
-
-
Save SaraM92/e170ab608a198fd945aa11b276a5d4e6 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
my_list = ["blue", "red", "green"] | |
#1- Using sort or srted directly or with specifc keys | |
my_list.sort() #sorts alphabetically or in an ascending order for numeric data | |
my_list = sorted(my_list, key=len) #sorts the list based on the length of the strings from shortest to longest. | |
# You can use reverse=True to flip the order | |
#2- Using locale and functools | |
import locale | |
from functools import cmp_to_key | |
my_list = sorted(my_list, key=cmp_to_key(locale.strcoll)) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment