Thursday, August 14, 2014

Python sort list of SNAPSHOT versions

Problem Description:
Return unexpected order when using sorted(unsorted_list, reverse=True) for snapshot versions. For example:
unsorted_list
[
'1.1.7-SNAPSHOT',
'1.1.6-SNAPSHOT',
'1.1.8-SNAPSHOT',
'1.1.9-SNAPSHOT',
'1.1.10-SNAPSHOT'
]

sorted(unsorted_list, reverse=True), return:
[
'1.1.9-SNAPSHOT',
'1.1.8-SNAPSHOT',
'1.1.7-SNAPSHOT',
'1.1.6-SNAPSHOT',
'1.1.10-SNAPSHOT'
]

However, the expected result we want:
[
'1.1.10-SNAPSHOT',
'1.1.9-SNAPSHOT',
'1.1.8-SNAPSHOT',
'1.1.7-SNAPSHOT',
'1.1.6-SNAPSHOT'
]

Existing Solutions:
There are some blogs which can handle above issue by sorting a list in a alphabetical order, links:
http://www.davekoelle.com/alphanum.html
http://blog.codinghorror.com/sorting-for-humans-natural-sort-order/
http://www.interact-sw.co.uk/iangblog/2007/12/13/natural-sorting

I tested this,
def sort_list(unsort_list):
    """
    Sort the given list in the way that humans expect.
    """
    convert = lambda text: int(text) if text.isdigit() else text
    alphanum_key = lambda key: [convert(c) for c in re.split('([0-9]+)', key)]
    unsort_list.sort(key=alphanum_key, reverse=True)
Easier Solution:
I tested another easier solution by using LooseVersion library, and it works well.
sorted(unsorted_list, key=LooseVersion, reverse=True)

Note: I have not compared different solutions, and I do not know which one is higher efficiency. But I read a blog, which compared a list of sorting method related with alphabetical human sorting, http://dave.st.germa.in/blog/2007/12/11/exception-handling-slow/. I really hope this will help someone who has this problem later.



No comments:

Post a Comment