2

How to sort list by alphanumeric values

a = ['v1_0005.jpg', 'v1_00015.jpg', 'v2_0007.jpg', 'v2_0002.jpg']

while sorting list using a.sort() I get

['v1_0005.jpg', 'v1_00015.jpg', 'v2_0007.jpg', 'v2_0002.jpg']

which means sorting order is wrong, I expect list is sort by alphanumeric value in python

Expected list to be :

['v2_0002.jpg','v1_0005.jpg','v2_0007.jpg', 'v1_00015.jpg']
2
  • Did you mean "while sorting list using a.sort() i get" (sort instead of list) ? Commented Jun 29, 2021 at 10:37
  • 3
    Note that 'v1_00017.jpg' is less than 'v1_0002.jpg', because the first differing character is '1' vs. '2', and '1' < '2'. If you want to do a more advanced sort, you'll need to extract the numeric portion from the string and convert it to a number. Commented Jun 29, 2021 at 10:37

3 Answers 3

1

You have to some techniques inorder to get the result that you want, ie you have to write a sorting function with the conditions.

So here you can make use of regex and lambda in python inorder to sort as you wish

import re
def sorted_nicely( l ):
    """ 
    Sorts the given iterable in the way that is expected.
 
    Required arguments:
    l -- The iterable to be sorted.
 
    """
    convert = lambda text: int(text) if text.isdigit() else text
    alphanum_key = lambda key: [convert(c) for c in re.split('([0-9]+)', key)]
    return sorted(l, key = alphanum_key)
              
# Driver code
a = ["v1_0001.jpg","v1_0002.jpg","v1_0003.jpg","v1_00017.jpg","v1_00015.jpg"]
print(sorted_nicely(a))

Output

['v1_0001.jpg', 'v1_0002.jpg', 'v1_0003.jpg', 'v1_00015.jpg', 'v1_00017.jpg']
Sign up to request clarification or add additional context in comments.

1 Comment

This method works only for same type of values in list A = ['v1_0001.jpg', 'v2_0002.jpg', 'v1_0003.jpg', 'v1_00015.jpg', 'v2_0007.jpg'] what to do for sorting
1

You have a problem: your list format is quite wrong - instead of v1_00015.jpg it should be v1_0015.jpg (no extra 0). Without that zero Python built-ins .sort() and sorted() work correctly. To transform the list, you can use this:

for i,e in enumerate(a):
    while len(a[i]) > 11:
        a[i] = e.replace('v1_0', 'v1_')

It replaces v1_0 to v1_ in every a item till item length == 11. Then just use a.sort():

a.sort()
print(a)

And it prints:

['v1_0001.jpg', 'v1_0002.jpg', 'v1_0003.jpg', 'v1_0015.jpg', 'v1_0017.jpg']

Comments

0

I'm assuming you want to use the numeric values. So you need to parse them out...
This can be done with a one-liner as so -

a = ['v1_0001.jpg', 'v1_00015.jpg', 'v1_00017.jpg', 'v1_0002.jpg', 'v1_0003.jpg']
a.sort(key=lambda x: int(x.split('v1_')[1].split('.jpg')[0]))
print(a)
# ['v1_0001.jpg', 'v1_0002.jpg', 'v1_0003.jpg', 'v1_00015.jpg', 'v1_00017.jpg']

2 Comments

What? is this a new question?? If so please explain...
You're combining questions from other posts. Please post one clear question, with expected output and example data. You might want to read the guide on writing SO questions...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.