Sort ArrayList from Major.Minor.Incremental Strings

I have an ArrayList version string that I would like to sort so that the first one is the first.

ArrayList<String> items = new ArrayList<String>();
items.add("4.1.0");
items.add("4.1.1");
items.add("4.1");
items.add("5.1");
items.add("4.0.1");

My expected result:

5.1
4.1.1
4.1.0
4.1
4.0.1

When considering values ​​as integers, "4.0.1" is greater than "4.1", which is not true for versions.

I am thinking of using a comparator interface for each major, minor, and incremental version. Is there an easier way to do this?

+4
source share
3 answers

Upen, There is a ComparableVersion class from maven for the same.

Also consider this SO <dialog .

Hope this solves your problem.

+3
source

, , , , SNAPSHOT . , .

maven ComparableVersion , maven jar .

, !

+1

Source: https://habr.com/ru/post/1623850/


All Articles