I have list of strings that look like this:
l = ["sometext-2022-21_sometext",
"sometext-2022-4_sometext",
"sometext-2022-121_sometext",
"sometext-2022-321_sometext",
"sometext-2022-1_sometext",
"sometext-2022-31_sometext"]
I want to sort it so that biggest number be on top, for example:
l = ["sometext-2022-321_sometext",
"sometext-2022-121_sometext",
"sometext-2022-31_sometext",
"sometext-2022-21_sometext",
"sometext-2022-4_sometext",
"sometext-2022-1_sometext"]
How can I do that, since sort does not work for this problem?
PS
In this case the string with biggest number is longest, but that does not have to be the case, strings can have different texts.
This is my real list of strings:
["https://essgfsgffghe.ch/docs/BS_Omni/BS_APG_001_AUS-2022-401_nodate.html",
"https://ensfgtsfgscheidsuche.ch/docs/BS_Omni/BS_APG_001_BEZ-2022-39_nodate.html",
"https://ensfgtsfguche.ch/docs/BS_Omni/BS_APG_001_VD-2022-5_nodate.html",
"https://egsfsfgtscheidsuche.ch/docs/BS_Omni/BS_APG_001_VD-2022-2_nodate.html",
"https://ensfgidsuche.ch/docs/BS_Omni/BS_APG_001_BES-2022-83_nodate.html",
"https://sfgfgnsfgche.ch/docs/BS_Omni/BS_SVG_001_IV-2022-54_nodate.html",
"https://entscsfghe.ch/docs/BS_Omni/BS_APG_001_BES-2022-36_nodate.html",
"https://essntscsfguche.ch/docs/BS_Omni/BS_APG_001_AUS-2022-32_nodate.html",
"https://entsfgeidsuche.ch/docs/BS_Omni/BS_APG_001_BES-2022-89_nodate.html",
"https://entfsfgsgsfuche.ch/docs/BS_Omni/BS_APG_001_AUS-2022-412_nodate.html",
"https://ensfgche.ch/docs/BS_Omni/BS_APG_001_VD-2022-5_nodate.html",
"https://ensfgse.ch/docs/BS_Omni/BS_APG_001_BES-2022-70_nodate.html",
"https://esfgche.ch/docs/BS_Omni/BS_APG_001_VD-2022-1_nodate.html"]
>Solution :
You may be able to use a regular expression as long as the pattern searched is not also contained in "sometext", resulting in false positives. list.sort takes a function that returns a key to use for sorting instead of of the original value in the list. In this case, we can convert string digits to a tuple of integers that will sort the way you want.
import re
l = ["sometext-2022-21_sometext",
"sometext-2022-4_sometext",
"sometext-2022-121_sometext",
"sometext-2022-321_sometext",
"sometext-2022-1_sometext",
"sometext-2022-31_sometext"]
def l_sort_key(s):
v1, v2 = re.search(r"-(\d+)-(\d+)_", s).groups()
return int(v1), int(v2)
l.sort(key=l_sort_key, reverse=True)
print(l)