Example usage for java.util Comparator Comparator

List of usage examples for java.util Comparator Comparator

Introduction

In this page you can find the example usage for java.util Comparator Comparator.

Prototype

Comparator

Source Link

Usage

From source file:Main.java

/**
 * Creates a map which is sorted by entry values.
 *   //from   w ww  .  j a  v  a2s.  c  o m
 * @param map the original map
 * 
 * @param <K> the key type
 * @param <V> the value type
 * 
 * @return a map which is by entry values.
 */
public static <K, V extends Comparable<? super V>> SortedSet<Map.Entry<K, V>> getEntriesSortedByValues(
        Map<K, V> map) {
    SortedSet<Map.Entry<K, V>> sortedEntries = new TreeSet<Map.Entry<K, V>>(new Comparator<Map.Entry<K, V>>() {
        @Override
        public int compare(Map.Entry<K, V> e1, Map.Entry<K, V> e2) {
            return e1.getValue().compareTo(e2.getValue());
        }
    });
    sortedEntries.addAll(map.entrySet());
    return sortedEntries;
}

From source file:Main.java

private static void mergeExeBatchFiles() {
    File file = new File(batchDir);
    System.out.println("debug:current path = " + file.getAbsolutePath());
    File[] files = file.listFiles();
    if (files == null || files.length == 0) {
        return;// w w  w .j  a v a  2  s. c o  m
    }
    Arrays.sort(files, new Comparator<File>() {
        public int compare(File file1, File file2) {
            if (file1.lastModified() > file2.lastModified()) {
                return 1;
            } else if (file1.lastModified() < file2.lastModified()) {
                return -1;
            } else {
                return 0;
            }
        }
    });
    StringBuffer command = new StringBuffer();
    for (File f : files) {
        command.append("call ").append(f.getAbsolutePath()).append("\n");
    }
    try {
        String filePath = batchDir + "build.bat";
        writeFile(filePath, command.toString());
        Runtime.getRuntime().exec("cmd /c start " + filePath);
    } catch (Exception e) {
        e.printStackTrace();
    }
}

From source file:Main.java

public static <K, V extends Comparable<? super V>> LinkedHashMap<K, V> sortMapByValue(Map<K, V> map,
        final boolean descending) {
    List<Map.Entry<K, V>> list = new LinkedList<Map.Entry<K, V>>(map.entrySet());
    Collections.sort(list, new Comparator<Map.Entry<K, V>>() {

        public int compare(Map.Entry<K, V> o1, Map.Entry<K, V> o2) {
            int comp = (o1.getValue()).compareTo(o2.getValue());

            if (descending) {
                comp = comp * (-1);/*from www  . ja v a 2 s.  c  om*/
            }

            return comp;
        }
    });

    LinkedHashMap<K, V> result = new LinkedHashMap<K, V>();
    for (Map.Entry<K, V> entry : list) {
        result.put(entry.getKey(), entry.getValue());
    }
    return result;
}

From source file:Main.java

public static <T> Comparator<T> inverseComparator(final Comparator<T> comp) {
    return new Comparator<T>() {
        public int compare(T o1, T o2) {
            return comp.compare(o2, o1);
        }/*from   ww w.j  ava  2  s . c  om*/
    };
}

From source file:Main.java

/**
 * Sort a map according to its values in ascending order when asc=true and i descending order otherwise.
 * @param unsortedMap //from  w w w .j  a  va  2 s .  c  o m
 * @param asc
 * @return
 */
public static Map<String, Integer> sortUsingComparator(Map<String, Integer> unsortedMap, final boolean asc) {

    // Convert Map to List
    List<Map.Entry<String, Integer>> list = new LinkedList<Map.Entry<String, Integer>>(unsortedMap.entrySet());

    // Sort a list using custom comparator, to compare the Map values
    Collections.sort(list, new Comparator<Map.Entry<String, Integer>>() {
        public int compare(Map.Entry<String, Integer> o1, Map.Entry<String, Integer> o2) {
            if (asc) {
                return (o1.getValue()).compareTo(o2.getValue());
            } else {
                return -1 * (o1.getValue()).compareTo(o2.getValue());
            }
        }
    });

    // Convert the sorted map back to a Map
    Map<String, Integer> sortedMap = new LinkedHashMap<String, Integer>();
    for (Iterator<Map.Entry<String, Integer>> it = list.iterator(); it.hasNext();) {
        Map.Entry<String, Integer> entry = it.next();
        sortedMap.put(entry.getKey(), entry.getValue());
    }
    return sortedMap;
}

From source file:Main.java

private static void sortFieldsByName(Field[] array) {
    Arrays.sort(array, new Comparator<Field>() {
        @Override//from w ww  .jav a2 s  .c  o  m
        public int compare(Field current, Field next) {
            String currentName = current.getName();
            String nextName = next.getName();
            return currentName.compareTo(nextName);
        }
    });
}

From source file:Main.java

public static <K, V> Comparator<Entry<K, V>> entryComparator(final Comparator<? super K> keyComparator) {
    return new Comparator<Entry<K, V>>() {
        @Override/*from   w  ww. j  a  v a 2s  .  com*/
        @SuppressWarnings("unchecked")
        // no less safe than putting it in the map!
        public int compare(Entry<K, V> a, Entry<K, V> b) {
            return (keyComparator == null) ? ((Comparable) a.getKey()).compareTo(b.getKey())
                    : keyComparator.compare(a.getKey(), b.getKey());
        }
    };
}

From source file:Main.java

public static String prepareFilePathForVideoSaveWithDraftUri(Uri draftUri) {
    String draftPath = draftUri.getPath();
    String draftMediaDirPath = draftPath.substring(0, draftPath.length() - 5);
    File draftMediaDir = new File(draftMediaDirPath);
    if (!draftMediaDir.exists()) {
        draftMediaDir.mkdirs();//from w ww  .  ja  v a  2  s . c  om
    }
    String[] files = draftMediaDir.list(new FilenameFilter() {
        @Override
        public boolean accept(File dir, String filename) {
            return filename.endsWith("-0.mp4") || filename.endsWith("-a.mp4");
        }
    });
    List<String> filePaths = Arrays.asList(files);
    Collections.sort(filePaths, new Comparator<String>() {
        @Override
        public int compare(String lhs, String rhs) {
            return rhs.compareTo(lhs);
        }
    });
    if (filePaths.size() > 0) {
        for (String file : filePaths) {
            return new File(draftMediaDir, file.substring(0, file.length() - 6) + ".mp4").getAbsolutePath();
        }
    }
    return new File(draftMediaDir, generateRandomFilename("mp4")).getAbsolutePath();
}

From source file:cherry.foundation.bizcal.BizYearUtil.java

public static Range<LocalDate> between(LocalDate from, LocalDate to) {
    return Range.between(from, to, new Comparator<LocalDate>() {
        @Override//from   w ww .  j a va2 s . co m
        public int compare(LocalDate o1, LocalDate o2) {
            return o1.compareTo(o2);
        }
    });
}

From source file:Main.java

/**
 * @return a comparator that provides the natural ordering (i.e. the order defined by T.compareTo(T)
 *//*from w  w w .j a  v  a  2  s .  co m*/
public static <T extends Comparable<T>> Comparator<T> naturalComparator() {
    return new Comparator<T>() {
        public int compare(T o1, T o2) {
            return o1.compareTo(o2);
        }
    };
}