How can I get the maximum version?

How can I sort an array of String

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

At my discretion, I do not mean here Converting it to Integer and getting 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 can get the maximum value if the array is like this.

 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 array of strings, but suppose that

 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 can i achieve this?

+4
source share
4 answers

This is basically Anny-Mousses answer in code: the limitation is that the version number only has numbers and dots.

 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); } } 
+2
source

It is not as easy as you think in general. Because there are version numbers like 3.1beta . I will not discuss them completely below, just draw a series of problems that arise.

But the main idea is this:

  • split string in . into an array, converting the individual components to integers.

  • compare component . If the two options match the first component, go to the next.

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

Now, if you want to support beta and the like, it gets really dirty. Take Debian version numbering for an example. In addition to points, it has an epoch separator. So, version 2:1.0 > 2.0 (the new era is larger than the previous era numbering). A - splits the revision number. So, 2-2 < 2.1-1 , because 2 < 2.1 (and the revision is secondary to the main version number!). There are 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 to read them. There is only the usual agreement that . highlights components with lower priority, and ~ is a popular indicator for pre-releases (therefore, it should be sorted to version without packages).

+5
source

Try it like

  Arrays.sort(s, new Comparator<String>() { @Override public int compare(String o1, String o2) { String[] a1 = o1.split("\\."); String[] a2 = o2.split("\\."); int c1 = Integer.parseInt(a1[0]) - Integer.parseInt(a2[0]); if (c1 != 0) { return c1; } return a1[1].compareTo(a2[1]); } }); 
+1
source

If instead of using Integer.parseInt (); you must use Double.parseDouble (); You can easily get the maximum value. I do not see any problems in this.

-1
source

All Articles