Example usage for java.lang Integer compareTo

List of usage examples for java.lang Integer compareTo

Introduction

In this page you can find the example usage for java.lang Integer compareTo.

Prototype

public int compareTo(Integer anotherInteger) 

Source Link

Document

Compares two Integer objects numerically.

Usage

From source file:AnimationTester.java

public static void main(String[] args) {

    Integer[] values = new Integer[] { 1, 2, 7, 3, 5 };
    Comparator<Integer> comp = new Comparator<Integer>() {
        public int compare(Integer d1, Integer d2) {
            return d1.compareTo(d2);
        }//w w w  . j  a  va2  s. c om
    };
    MergeSorter.sort(values, comp);
    for (int i = 0; i < values.length; i++) {
        System.out.print(values[i] + " ");
    }

}

From source file:QuickSort.java

/**
 * @param args//from www. ja  v  a 2 s .co  m
 */
public static void main(String[] args) {
    Integer[] array = new Integer[15];
    Random rng = new Random();
    int split = 10;
    for (int i = 0; i < split; i++) {
        array[i] = new Integer(rng.nextInt(100));
    }
    for (int i = split; i < array.length; i++) {
        array[i] = null;
    }

    System.out.println(Arrays.toString(array));

    QuickSort.sort(array, new Comparator<Integer>() {
        @Override
        public int compare(Integer o1, Integer o2) {
            return o1.compareTo(o2);
        };
    }, 0, split - 1);

    System.out.println(Arrays.toString(array));

}

From source file:com.datazuul.iiif.presentation.api.ManifestGenerator.java

public static void main(String[] args)
        throws ParseException, JsonProcessingException, IOException, URISyntaxException {
    Options options = new Options();
    options.addOption("d", true, "Absolute file path to the directory containing the image files.");

    CommandLineParser parser = new DefaultParser();
    CommandLine cmd = parser.parse(options, args);

    if (cmd.hasOption("d")) {
        String imageDirectoryPath = cmd.getOptionValue("d");
        Path imageDirectory = Paths.get(imageDirectoryPath);
        final List<Path> files = new ArrayList<>();
        try {//w  ww  .j a v a  2  s . c  o  m
            Files.walkFileTree(imageDirectory, new SimpleFileVisitor<Path>() {
                @Override
                public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
                    if (!attrs.isDirectory()) {
                        // TODO there must be a more elegant solution for filtering jpeg files...
                        if (file.getFileName().toString().endsWith("jpg")) {
                            files.add(file);
                        }
                    }
                    return FileVisitResult.CONTINUE;
                }
            });
        } catch (IOException e) {
            e.printStackTrace();
        }
        Collections.sort(files, new Comparator() {
            @Override
            public int compare(Object fileOne, Object fileTwo) {
                String filename1 = ((Path) fileOne).getFileName().toString();
                String filename2 = ((Path) fileTwo).getFileName().toString();

                try {
                    // numerical sorting
                    Integer number1 = Integer.parseInt(filename1.substring(0, filename1.lastIndexOf(".")));
                    Integer number2 = Integer.parseInt(filename2.substring(0, filename2.lastIndexOf(".")));
                    return number1.compareTo(number2);
                } catch (NumberFormatException nfe) {
                    // alpha-numerical sorting
                    return filename1.compareToIgnoreCase(filename2);
                }
            }
        });

        generateManifest(imageDirectory.getFileName().toString(), files);
    } else {
        // automatically generate the help statement
        HelpFormatter formatter = new HelpFormatter();
        formatter.printHelp("ManifestGenerator", options);
    }
}

From source file:com.sec.ose.osi.util.tools.Tools.java

public static ArrayList<String> sortByValue(final HashMap<String, Integer> map) {
    ArrayList<String> key = new ArrayList<String>();
    key.addAll(map.keySet());/*from  w ww.  j av  a 2 s  .co m*/
    Collections.sort(key, new Comparator<Object>() {
        public int compare(Object o1, Object o2) {
            Integer v1 = map.get(o1);
            Integer v2 = map.get(o2);
            return v1.compareTo(v2);
        }
    });
    Collections.reverse(key);
    return key;
}

From source file:net.firejack.platform.core.utils.SortFileUtils.java

/**
 * @param files//from w w w . ja v a2 s  . co  m
 * @param desc
 * @return
 */
public static File[] sortingByExt(File[] files, boolean desc) {
    Arrays.sort(files, new Comparator() {
        public int compare(final Object o1, final Object o2) {
            String s1 = ((File) o1).getName().toLowerCase();
            String s2 = ((File) o2).getName().toLowerCase();
            final int s1Dot = s1.lastIndexOf('.');
            final int s2Dot = s2.lastIndexOf('.');
            //
            if ((s1Dot == -1) == (s2Dot == -1)) { // both or neither
                s1 = s1.substring(s1Dot + 1);
                s2 = s2.substring(s2Dot + 1);
                Integer i1 = Integer.parseInt(s1);
                Integer i2 = Integer.parseInt(s2);
                return i1.compareTo(i2);
            } else if (s1Dot == -1) { // only s2 has an extension, so s1 goes first
                return -1;
            } else { // only s1 has an extension, so s1 goes second
                return 1;
            }
        }
    });
    if (desc) {
        org.apache.commons.lang.ArrayUtils.reverse(files);
    }
    return files;
}

From source file:io.fabric8.maven.core.util.VersionUtil.java

/**
 * Compares two version strings such that "1.10.1" > "1.4" etc
 *//*from w w w .  j  a v  a 2s .co m*/
public static int compareVersions(String v1, String v2) {
    String[] components1 = split(v1);
    String[] components2 = split(v2);
    int diff;
    int length = Math.min(components1.length, components2.length);
    for (int i = 0; i < length; i++) {
        String s1 = components1[i];
        String s2 = components2[i];
        Integer i1 = tryParseInteger(s1);
        Integer i2 = tryParseInteger(s2);
        if (i1 != null && i2 != null) {
            diff = i1.compareTo(i2);
        } else {
            // lets assume strings instead
            diff = s1.compareTo(s2);
        }
        if (diff != 0) {
            return diff;
        }
    }
    diff = Integer.compare(components1.length, components2.length);
    if (diff == 0) {
        if (v1 == v2) {
            return 0;
        }
        /* if v1 == null then v2 can't be null here (see 'if' above).
           So for v1 == null its always smaller than v2 */;
        return v1 != null ? v1.compareTo(v2) : -1;
    }
    return diff;
}

From source file:org.kuali.rice.core.api.util.VersionHelper.java

private static int compareVersionInt(String versionOne, String versionTwo) throws IllegalArgumentException {

    if (StringUtils.isBlank(versionOne) || StringUtils.isBlank(versionTwo)) {
        throw new IllegalArgumentException("Invalid argument");
    }/*from  w w  w  . j  a v a 2s .co m*/

    Matcher m1 = extractVersion.matcher(versionOne);
    Matcher m2 = extractVersion.matcher(versionTwo);

    if (!m1.matches() || !m2.matches()) {
        throw new IllegalArgumentException("Unable to extract version number from string using regex");
    }

    //uses the matches generated by the extractVersion pattern to pull the version out of a string
    //  i.e. "2.1.3-SNAPSHOT" after sanitization is "2.1.3"

    String sanitizedVOne = versionOne.substring(m1.start(2), m1.end(2));
    String sanitizedVTwo = versionTwo.substring(m2.start(2), m2.end(2));

    //uses the splitPattern to break a string into an array of numeric strings
    // i.e. "2.1.3" becomes {"2", "1", "3"}

    String[] oneDigits = splitVersion.split(sanitizedVOne);
    String[] twoDigits = splitVersion.split(sanitizedVTwo);

    // if there are unequal arrays, iterate over arrays up to the length of the shorter array
    // have to be able to deal with the case "2.1" compared to "2.1.3"
    int length = 0;
    if (oneDigits.length < twoDigits.length) {
        length = oneDigits.length;
    } else {
        length = twoDigits.length;
    }

    for (int i = 0; i < length; i++) {
        Integer intOne = Integer.valueOf(oneDigits[i]);
        Integer intTwo = Integer.valueOf(twoDigits[i]);
        Integer compare = intOne.compareTo(intTwo);
        if (compare < 0) {
            return -1;
        } else if (compare > 0) {
            return 1;
        }
    }
    if (oneDigits.length < twoDigits.length) {
        return -1;
    }
    if (oneDigits.length == twoDigits.length) {
        return 0;
    }
    return 1;

}

From source file:org.polymap.rhei.batik.PanelSite.java

protected static Integer check(Integer v1, Integer v2, int falseExpected, String b1, String b2) {
    if (v1.compareTo(v2) == falseExpected) {
        String op = falseExpected < 0 ? "<" : ">";
        throw new IllegalArgumentException(
                Joiner.on(" ").join("Illegal value:", b1, op, b2, "(", v1, op, v2, ")"));
    }//from  w w  w .  j  a v a 2s  .co  m
    return v1;
}

From source file:org.openlegacy.rpc.support.binders.RpcPartsBinder.java

private static void filterNullObjects(String namespace, String partName,
        SimpleRpcPojoFieldAccessor fieldAccesor, Object[] objects, List<Integer> emptyObjectsIndex) {

    Collections.sort(emptyObjectsIndex, new Comparator<Integer>() {

        @Override//  w ww  .  j  a v a2  s .  c o  m
        public int compare(Integer o1, Integer o2) {
            return o2.compareTo(o1);
        }
    });
    for (Integer index : emptyObjectsIndex) {
        objects = ArrayUtils.remove(objects, index);
    }
    fieldAccesor.setPartFieldValue(namespace, partName, objects);
}

From source file:org.broad.igv.util.Utilities.java

static public Comparator getNumericStringComparator() {

    Comparator comparator = new Comparator<String>() {

        public int compare(String s1, String s2) {
            StringTokenizer st1 = new StringTokenizer(s1, " ");
            StringTokenizer st2 = new StringTokenizer(s2, " ");

            while (st1.hasMoreTokens() && st2.hasMoreTokens()) {
                String t1 = st1.nextToken();
                String t2 = st2.nextToken();

                int c;

                try {
                    Integer i1 = new Integer(t1);
                    Integer i2 = new Integer(t2);

                    c = i1.compareTo(i2);
                } catch (NumberFormatException e) {
                    c = t1.compareTo(t2);
                }/*from  ww  w . ja  v  a2 s. co  m*/

                if (c != 0) {
                    return c;
                }
            }

            return 0;
        }
    };

    return comparator;
}