Sorting a list of dot-separated numbers, like software versions
Split each version string to compare it as a list of integers:
versions_list.sort(key=lambda s: map(int, s.split('.')))
Gives, for your list:
['1.0.0', '1.0.2', '1.0.12', '1.1.2', '1.3.3']
In Python3 map
no longer returns a list
, So we need to wrap it in a list
call.
versions_list.sort(key=lambda s: list(map(int, s.split('.'))))
The alternative to map here is a list comprehension. See this post for more on list comprehensions.
versions_list.sort(key=lambda s: [int(u) for u in s.split('.')])
How to sort list of version tags in Python
You can use sorted
with a key
.
You indeed need to use str.split
to split the string into tokens. This will allow you to rely on list ordering.
Although, you will need to cast strings of digits to int
first to get the expected result, otherwise numbers will be sorted in alphabetical order instead of numercial order.
Example
l = ['1.0.0','1.0.1','1.1.1','1.1.1.abc','2.20.11','2.0.10.abc2']
sorted_l = sorted(l, key=lambda x: [int(i) if i.isdigit() else i for i in x.split('.')])
Output
['1.0.0', '1.0.1', '1.1.1', '1.1.1.abc', '2.0.10.abc2', '2.20.11']
Specific sort a list of numbers separated by dots
Just get the last part, convert that to an int and return it as the key for comparison
print(sorted(L, key=lambda x: int(x.split(".")[2])))
If you want all the parts to be considered, you can do like this
print(sorted(L, key=lambda x: [int(i) for i in x.rstrip(".").split(".")]))
It removes .
at the end of the strings, splits them based on .
and then converts each and every part of it to an int
. The returned list will be used for comparison.
You can read more about how various sequences will be compared by Python, here
Output
['1.1.1.','1.1.2.','1.1.3.','1.1.4.','1.1.10.','1.1.11.','1.1.12.','1.1.13.']
Racket: how to sort dot-separated numbers
Split each string up on #\.
and turn it into a list of numbers, and sort based on that transformation. Example using SRFI-67 to compare lists:
#lang racket
(require srfi/67)
(define versions '("1.1.2" "1.0.0" "1.12.1" "1.3.3" "1.0.7" "1.0.2"))
(define (sort-versions vlst)
(sort vlst (lambda (a b) (< (list-compare integer-compare a b) 0))
#:key (lambda (v) (map string->number (string-split v ".")))
#:cache-keys? #t))
(writeln (sort-versions versions))
How to sort a version list?
Supply a key
to the sorted
function:
sorted(mylist, key=lambda v: map(int, v.split('.')))
Sort version-dotted number strings in Javascript?
You could prepend all parts to fixed size strings, then sort that, and finally remove the padding again.
var arr = ['5.5.1', '4.21.0', '4.22.0', '6.1.0', '5.1.0', '4.5.0'];arr = arr.map( a => a.split('.').map( n => +n+100000 ).join('.') ).sort() .map( a => a.split('.').map( n => +n-100000 ).join('.') );
console.log(arr)
How can i sort semantic versions in pandas?
Pandas solution with sorted
, StrictVersion solution and assign to column:
print (df)
ver
0 0.1
1 0.2
2 0.10
3 0.2.1
4 0.3
5 0.10.1
from distutils.version import StrictVersion
df['ver'] = sorted(df['ver'], key=StrictVersion)
print (df)
ver
0 0.1
1 0.2
2 0.2.1
3 0.3
4 0.10
5 0.10.1
EDIT:
For sort index is possible use reindex
:
print (df)
a b
ver
0.1 1 q
0.2 2 w
0.10 3 e
0.2.1 4 r
0.3 5 t
0.10.1 6 y
from distutils.version import StrictVersion
df = df.reindex(index=pd.Index(sorted(df.index, key=StrictVersion)))
print (df)
a b
0.1 1 q
0.2 2 w
0.2.1 4 r
0.3 5 t
0.10 3 e
0.10.1 6 y
Sorting complex list of strings in python
This should do it:
sorted(l, key=lambda k: [int(num) for num in k.split('.')])
How do I sort a list of section numbers in Python?
Use a custom compare function that converts the strings into sub-lists of integers. Those will sort correctly without problems.
In [4]: ls = ['1.1', '1.10', '1.2', '1.2.3', '1.2.1', '1.9']
In [5]: def section(s):
...: return [int(_) for _ in s.split(".")]
...:
In [6]: sorted(ls, key=section)
Out[6]: ['1.1', '1.2', '1.2.1', '1.2.3', '1.9', '1.10']
Related Topics
Log into Gmail Using Selenium in Python
How to Use Multiprocessing Queue in Python
Absolute VS. Explicit Relative Import of Python Module
Keyerror: 'Tcl_Library' When I Use Cx_Freeze
Save Classifier to Disk in Scikit-Learn
How to Convert CSV File to Multiline JSON
How to Convert a String Date into Datetime Format in Python
How to Find Most Common Elements of a List
Optimal Hdf5 Dataset Chunk Shape for Reading Rows
How to Escape Strings for SQLite Table/Column Names in Python
Date Ticks and Rotation in Matplotlib
How to Change Values in a Tuple
Ssl.Sslerror: Tlsv1 Alert Protocol Version