The standard sort that comes installed on OS X can sort by fields using a separator. So you can sort the version numbers and any suffixes.
This will sort by suffix first and then by the X.Y.Z parts sort -s -t- -k 2,2n | sort -t. -s -k 1,1n -k 2,2n -k 3,3n -k 4,4n
, which can also sort the -N-g format version number from the git describe --tags
command
0.11.1
0.11.4
0.11.9-1-ge6b0c59
0.12.0
0.12.1
0.12.2-1-g2d0a334
0.13.0
0.13.0-1-g7711b16
0.13.0-2-g32f91bd
0.13.0-3-g83e21c5
0.14.1-alpha
0.14.1
0.14.2
The -3-g83e21c5 above is an example of a suffix that the git describe --tags
command will automatically append to the latest tag to to signify the number of commits since the tag (3), and the Git SHA hash of the most recent commit (83e21c5)
To reverse the sort into descending order do this: sort -s -t- -k 2,2nr | sort -t. -s -k 1,1nr -k 2,2nr -k 3,3nr -k 4,4nr
Or you can define a shell function around it.
version_sort() {
# read stdin, sort by version number descending, and write stdout
# assumes X.Y.Z version numbers
# this will sort tags like pr-3001, pr-3002 to the END of the list
# and tags like 2.1.4 BEFORE 2.1.4-gitsha
sort -s -t- -k 2,2nr | sort -t. -s -k 1,1nr -k 2,2nr -k 3,3nr -k 4,4nr
}
or write it into a little file named version-sort, and put into some directory on your PATH. Be sure to chmod +x
on the file
#!/usr/bin/env bash
sort -s -t- -k 2,2nr | sort -t. -s -k 1,1nr -k 2,2nr -k 3,3nr -k 4,4nr