Advertisement
apad464

Elevator Maintenance

Apr 11th, 2022
103
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.32 KB | None | 0 0
  1. import java.util.Arrays;
  2. import java.util.Comparator;
  3.  
  4. class Solution implements Comparator<String> {
  5.   @Override
  6.   public int compare(String version1, String version2) {
  7.    
  8.     //separates by major, minor and revision types
  9.     String[] v1 = version1.split("\\.");
  10.     String[] v2 = version2.split("\\.");
  11.    
  12.     //saves number of both versions
  13.     int major1 = major(v1);
  14.     int major2 = major(v2);
  15.     Integer minor1 = minor(v1);
  16.     Integer minor2 = minor(v2);
  17.     Integer revision1 = revision(v1);
  18.     Integer revision2= revision(v2);
  19.  
  20.     //returns the bigger version
  21.     if (major1 == major2) {
  22.         if (minor1 == minor2) {
  23.           return revision1.compareTo(revision2);
  24.         }
  25.       return minor1.compareTo(minor2);
  26.     }
  27.     return major1 > major2 ? 1 : -1;
  28.   }
  29.  
  30.   //returns the major
  31.   private int major(String[] version) {
  32.     return Integer.parseInt(version[0]);
  33.   }
  34.  
  35.   //returns the minor
  36.   private Integer minor(String[] version) {
  37.     return version.length > 1 ? Integer.parseInt(version[1]) : -1;
  38.   }
  39.  
  40.   //returns the revision
  41.   private Integer revision(String[] version) {
  42.       return version.length > 2 ? Integer.parseInt(version[2]) : -1;
  43.   }
  44.  
  45.     public static String[] solution(String[] l) {
  46.         Arrays.sort(l, new Solution());
  47.         return l;
  48.     }
  49.    
  50. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement