Consider this list of version numbers:
0.3.0
0.3.1
...
0.3.8
0.3.9
0.3.10
0.3.11
git tag
would print them in the following ord
Easier solution:
serv ~: echo -e "1.1.1\n1.3.2\n1.1.10\n1.1.2" | sort -V
1.1.1
1.1.2
1.1.10
1.3.2
Breakdown of the sort
options being used here:
You will soon (with Git 1.9.x/2.0, Q2 2014) be able to use git only for getting the right sorted output:
See commit b6de0c6, from commit 9ef176b, authored by Nguyễn Thái Ngọc Duy (pclouds):
--sort=<type>
Sort in a specific order.
Supported type is:
- "
refname
" (lexicographic order),- "
version:refname
" or "v:refname
" (tag names are treated as versions).Prepend "
-
" to reverse sort order.
In your case:
git tag -l --sort=version:refname
A few test cases:
git tag foo1.3 &&
git tag foo1.6 &&
git tag foo1.10
Here is what you would get:
# lexical sort
git tag -l --sort=refname "foo*"
foo1.10
foo1.3
foo1.6
# version sort
git tag -l --sort=version:refname "foo*" >actual &&
foo1.3
foo1.6
foo1.10
# reverse version sort
git tag -l --sort=-version:refname "foo*" >actual &&
foo1.10
foo1.6
foo1.3
# reverse lexical sort
git tag -l --sort=-refname "foo*" >actual &&
foo1.6
foo1.3
foo1.10
serv ~: echo -e "1.1.1\n1.3.2\n1.1.10\n1.1.2" | sort -n -t. -k1,1 -k2,2 -k3,3
1.1.1
1.1.2
1.1.10
1.3.2
Breakdown of the sort
options being used here:
-n
- sort using numerical string order (thus 10
comes after 1
)-t.
- use periods as field separators-k1,1
define a sort key on the first field (and only the first field)-k2,2
define a sort key on the second field (and only the second field)-k3,3
define a sort key on the third field (and only the third field)