Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Sorting list of strings that contain number with Python

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.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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)
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading