JSS
JSS

Reputation: 2183

Getting Max value from x.y.z formatted Number

Is there an easy way of finding the MAX number from the list where number is stored in x.y.z format? e.g. To manage some system versions.

I have tried Collection.max(list) and that does not work.

Sample Code:

public static void main(String args[])
{
    List<String> list = new ArrayList<String>();

    list.add("1.0.0");
    list.add("1.1.0");
    list.add("1.9.0");
    list.add("1.10.0");

    System.out.println(Collections.max(list));
}

Expected: 1.10.0
Result: 1.9

Thanks for your time.

Upvotes: 2

Views: 438

Answers (5)

amukhachov
amukhachov

Reputation: 5900

Try to use this one :

Collections.max(myList, new Comparator<String>() {

        @Override
        public int compare(String lhs, String rhs) {
            String[] first = lhs.split("\\.");
            String[] second = rhs.split("\\.");
            for (int i = 0; i < first.length; i++) {
                if(Integer.valueOf(first[i]) > Integer.valueOf(second[i])) {
                    return 1;
                }
                if(Integer.valueOf(first[i]) < Integer.valueOf(second[i])) {
                    return -1;
                }
            }
            return 0;
        }
    });

Upvotes: 2

Aleks G
Aleks G

Reputation: 57316

You can use version of max with the specified comparator:

System.out.println(Collections.max(list, new Comparator<String>() {
    public int compare(String s1, String s2)
    {
        StringTokenizer st1 = new StringTokenizer(s1,".");
        StringTokenizer st2 = new StringTokenizer(s2,".");
        int res = 0;
        String t1, t2;
        while(st1.hasMoreTokens() && st2.hasMoreTokens())
        {
            t1 = st1.nextToken(); 
            t2 = st2.nextToken();
            res = Integer.valueOf(t1).compareTo(Integer.valueOf(t2));
        }
        if(res == 0)
        {
            res = st1.hasMoreTokens() ? 1 : (st2.hasMoreTokens() ? -1 : 0);
        }
        return res;

    }
    public boolean equals(Object obj) { return false; }
}));

Upvotes: 1

buc
buc

Reputation: 6358

You may have to write a custom Comparator for comparing version number strings:

public class VersionComparator extends Comparator<String> {

    @Override
    public int compare(String o1, String o2) {
        // Get major/minor/revison numbers by splitting strings at dots
        String[] p1 = o1.split("\\.");
        String[] p2 = o2.split("\\.");

        // Compare major versions then minor then revision until a difference found
        for(int i = 0; i < (p1.length < p2.length) ? p1.length : p2.length; i++) {
            int result = Integer.valueOf(p1[i]).compareTo(Integer.valueOf(p2[i]));
            if(result != 0) return result;
        }

        // Return zero if they're identical
        return 0;
    }
}

The you can use this comparator with the Collections.max function:

Collections.max(list, new VarsionComparator());

Upvotes: 2

Andrzej Doyle
Andrzej Doyle

Reputation: 103787

Well for one thing, you need to ensure that Java knows they are numbers - at the moment they're just Strings, and strings sort lexigraphically (i.e. in "alphabetical order").

My approach to this would be to create a small class that implements Comparable, which will then work automatically with sorting and comparison logic. Something like this perhaps:

public class VersionNumber implements Comparable<VersionNumber> {
    public final int major;
    public final int minor;
    public final int patch;

    // Constructor etc. elided

    public int compareTo(VersionNumber other) {
        if (other.major != major) return major - other.major;
        if (other.minor != minor) return minor - other.minor;
        return patch - other.patch;
    }
}

Parsing the string to create instances of this class is left as an exercise to the reader!

Upvotes: 2

gprathour
gprathour

Reputation: 15333

This will give you 1.9 because it will not consider second number to be 10, it will treat it as 1 first and then 9

Edit

If you want to do it manually, then

  1. Split your number on basis of "."
  2. Check manually which number is greater.

Upvotes: 0

Related Questions