Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Arrays;
- import java.util.Comparator;
- class Solution implements Comparator<String> {
- @Override
- public int compare(String version1, String version2) {
- //separates by major, minor and revision types
- String[] v1 = version1.split("\\.");
- String[] v2 = version2.split("\\.");
- //saves number of both versions
- int major1 = major(v1);
- int major2 = major(v2);
- Integer minor1 = minor(v1);
- Integer minor2 = minor(v2);
- Integer revision1 = revision(v1);
- Integer revision2= revision(v2);
- //returns the bigger version
- if (major1 == major2) {
- if (minor1 == minor2) {
- return revision1.compareTo(revision2);
- }
- return minor1.compareTo(minor2);
- }
- return major1 > major2 ? 1 : -1;
- }
- //returns the major
- private int major(String[] version) {
- return Integer.parseInt(version[0]);
- }
- //returns the minor
- private Integer minor(String[] version) {
- return version.length > 1 ? Integer.parseInt(version[1]) : -1;
- }
- //returns the revision
- private Integer revision(String[] version) {
- return version.length > 2 ? Integer.parseInt(version[2]) : -1;
- }
- public static String[] solution(String[] l) {
- Arrays.sort(l, new Solution());
- return l;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement