Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How I can get the maximum version?

Tags:

java

How can I sort the following array, String?

String[] s = {"0.1", "0.3", "0.6", "0.4", "0.5", "0.2", "0.7", "0.8", "0.9", "0.10"};

By sort I don't mean here converting it into in an integer and get the result as 0.9.

Here I want to get the value as 0.10.

Here in this case if the string array contains 1.1 then the maximum value will be 1.1.

I am able to get the maximum value if the array is like this, i.e.,

String[] s = {"0.1", "0.4", "0.3", "0.4", "0.5", "0.2", "0.7", "1.8", "2.9", "3.1"};

My code will work for this string array, but suppose if the

String[] s = {"0.1", "1.4", "1.3", "0.4", "0.5", "0.2", "2.7", "1.8", "2.9", "0.1"};

My code.

public String createNewVersion(
            String[] entityVersionHistory) {

Map<Integer, List<Integer>> m_Map1 = new HashMap<Integer, List<Integer>>();

String prevKey = "0";
String currentKey = null;

List<Integer> list = new ArrayList<Integer>();

for (String str: entityVersionHistory)
{
    String[] splitVersion = str.split("\\.");
    currentKey = splitVersion[0];
    if(!prevKey.equals(currentKey))
    {
        Integer s = new Integer(splitVersion[1]);

        m_Map1.put(Integer.valueOf(prevKey), list);
        list = new ArrayList<Integer>();
        list.add(s);

        prevKey = currentKey;
    }
    else
    {
        Integer s = new Integer(splitVersion[1]);
        list.add(s);
    }
}
m_Map1.put(Integer.valueOf(prevKey), list);

How I can achieve this?

like image 352
vikiiii Avatar asked Feb 17 '23 23:02

vikiiii


2 Answers

It's not as easy as you think in general. Because there are version numbers such as 3.1beta, too. I'm not going to discuss these fully below, just sketch a number of challenges that arise.

But the basic idea is as follows:

  1. split the string at . into an array, converting the individual components into integers.

  2. compare component-wise. If two numbers agree on the first component, skip to the next.

So, given the version numbers 3.10.1 and 3.9.2 we convert them first to an array of integers: { 3, 10, 1 } and {3, 9, 2}. Then we test the first component, but 3 == 3, so we skip to the next component. 10 > 9, so the result is that the first is larger.

Now if you want to support beta and similar things, it gets really messy. Take Debian version numbering for examples. In addition to dots, it has an epoch separator. So a verision of 2:1.0 > 2.0 (a new epoch is larger than the previous epochs numbering). A - separates a revision number. So 2-2 < 2.1-1, because 2 < 2.1 (and the revision is secondary to the main version number!). There are also negative versions. So 1.0~beta < 1.0, but 1.0final > 1.0 - read this as "1.0 minus beta" and "1.0 plus final".

There is no unique standard for how these are to be read. There is just the common convention that . separate lower-priority components, while ~ is a popular indicator for pre-releases (which is why it should sort before the non-postfixed version).

like image 182
Has QUIT--Anony-Mousse Avatar answered Feb 28 '23 03:02

Has QUIT--Anony-Mousse


This is basically Anny-Mousse's answer in code: The restriction is, there are only digits and dots allowed in a version number.

public class Version implements Comparable<Version> {

    private int[] version;

    public Version(String str) {
        if (!str.matches("\\d+[.\\d]*?\\d")) {
            throw new IllegalArgumentException(
                    "Version must start and end with digit and"
                    + "only contain digits and dots."
                    + " You provided '" + str + "'");
        }
        String[] tokens = str.split("\\.");
        version = new int[tokens.length];
        for (int i = 0; i < tokens.length; i++) {
            version[i] = Integer.parseInt(tokens[i], 10);
        }
    }

    @Override
    public int compareTo(Version other) {
        Version shorterOne =
                this.version.length < other.version.length ?
                this : other;
        int min = shorterOne.version.length;
        for (int i = 0; i < min; i++) {
            if (this.version[i] != other.version[i]) {
                return this.version[i] - other.version[i];
            }
        }
        return this.version.length - other.version.length;
    }

    @Override
    public String toString() {
        StringBuilder str = new StringBuilder(2 * version.length);
        for (Integer i : version) {
            str.append(i).append('.');
        }
        return str.deleteCharAt(str.length() - 1).toString();
    }

    public static void main(String[] args) {
        String[] s = {"1.4","1.3","0.4","0.5","0.2","2.7","1.8","2.9","0.1"};
        List<Version> list = new ArrayList<>(s.length);
        for (String str : s) {
            list.add(new Version(str));
        }
        Version max = Collections.max(list);
        System.out.println(max);
    }
}
like image 24
jlordo Avatar answered Feb 28 '23 01:02

jlordo