Sort versions specified as Strings

Last modified by Vincent Massol on 2026/08/05 17:14

cogSort strings representing software versions
TypeSnippet
CategoryOther
Developed by

Vincent Massol

Rating
0 Votes
LicenseGNU Lesser General Public License 2.1

Description

Example to sort a list

{{velocity}}
#set ($list = ["10.0.1", "other", "10.1", "9.5"])
#set ($versionList = [])
#foreach ($item in $list)
  #set ($version = $services.extension.parseVersion($item))
  #set ($discard = $versionList.add($version))
#end
#set ($sortedList = $collectiontool.sort($versionList))
$versionList
$sortedList
{{/velocity}}

Results:

[10.0.1, other, 10.1, 9.5]
[other, 9.5, 10.0.1, 10.1]

Example to sort a map

{{velocity}}
#set ($list = ["10.0.1", "other", "10.1", "9.5"])
#set ($versionMap = $collectiontool.getSortedMap())
#foreach ($item in $list)
  #set ($version = $services.extension.parseVersion($item))
  #set ($discard = $versionMap.put($version, $foreach.count))
#end
Before: $list
After: $versionMap
{{/velocity}}

Results:

Before: [10.0.1, other, 10.1, 9.5]
After: {other=2, 9.5=4, 10.0.1=1, 10.1=3}

Get Connected