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

public static List sortMapByKeyDESC(Map map) {
    List list = new ArrayList(map.entrySet());

    Collections.sort(list, new Comparator() {
        public int compare(Object o1, Object o2) {
            return ((Comparable) ((Map.Entry) (o2)).getKey()).compareTo(((Map.Entry) (o1)).getKey());
        }/*from  w w  w . j  a  v  a  2 s  .c om*/
    });

    return list;
}

From source file:Main.java

public static List sortMapByValueASC(Map map) {
    List list = new ArrayList(map.entrySet());

    Collections.sort(list, new Comparator() {
        public int compare(Object o1, Object o2) {
            return ((Comparable) ((Map.Entry) (o1)).getValue()).compareTo(((Map.Entry) (o2)).getValue());
        }// ww w  . j  ava2 s.  c o  m
    });

    return list;
}

From source file:Main.java

public static List sortMapByValueDESC(Map map) {
    List list = new ArrayList(map.entrySet());

    Collections.sort(list, new Comparator() {
        public int compare(Object o1, Object o2) {
            return ((Comparable) ((Map.Entry) (o2)).getValue()).compareTo(((Map.Entry) (o1)).getValue());
        }/*from www.  j  a v a 2 s  .c  om*/
    });

    return list;
}

From source file:Main.java

public static <T> void sortWithIndexesComparator(final List<T> list, final Comparator<Integer> comparator) {
    Collections.sort(list, new Comparator<T>() {
        @Override/*w w w  .j ava2  s  . com*/
        public int compare(T lhs, T rhs) {
            return comparator.compare(list.indexOf(lhs), list.indexOf(rhs));
        }
    });
}

From source file:Main.java

public static File[] dirListByDescendingDate(File[] files) {
    Arrays.sort(files, new Comparator() {
        public int compare(final Object o1, final Object o2) {
            return new Long(((File) o2).lastModified()).compareTo(new Long(((File) o1).lastModified()));
        }// www.j  a va2  s.co m
    });
    return files;
}

From source file:Main.java

public static <C extends Comparable<C>> Comparator<List<C>> getListComparator() {
    return new Comparator<List<C>>() {
        public int compare(List<C> list1, List<C> list2) {
            return compareLists(list1, list2);
        }//from www .  j av a 2 s .c o m
    };
}

From source file:Main.java

public static String[] randomizeArray(String[] array) {

    Arrays.sort(array, new Comparator<String>() {

        private final Random generator = new Random();

        @Override//  w ww  . jav a2  s  . c  o m
        public int compare(String lhs, String rhs) {
            // returns a random int between 1 and -1
            return (generator.nextInt(1) * 2) - 1;
        }
    });
    return array;
}

From source file:Main.java

public static <C extends Comparable<C>> Comparator<List<C>> getListComparator() {
    return new Comparator<List<C>>() {
        @Override//from  ww w.  j ava 2s  . c  o m
        public int compare(List<C> list1, List<C> list2) {
            return compareLists(list1, list2);
        }
    };
}

From source file:Main.java

public static <K, V extends Comparable<? super V>> Map<K, V> sortByValue(Map<K, V> map) {
    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) {
            return (o2.getValue()).compareTo(o1.getValue());
        }//from  w  ww. j ava 2s .  c  om
    });
    Map<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 List<Map.Entry<Long, Long>> sortEntrySetToList(Set<Map.Entry<Long, Long>> set) {
    List<Map.Entry<Long, Long>> list = new LinkedList<>(set);
    Collections.sort(list, new Comparator<Map.Entry<Long, Long>>() {

        @Override/* ww w  .j  av  a2  s . c  om*/
        public int compare(Map.Entry<Long, Long> o1, Map.Entry<Long, Long> o2) {
            if (o1.getValue() > o2.getValue()) {
                return 1;
            }
            if (o1.getValue() < o2.getValue()) {
                return -1;
            }
            return 0;
        }
    });
    return list;
}