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 <T> Comparator<Collection<? extends T>> listComparator(
        final Comparator<? super T> elementComparator) {
    return new Comparator<Collection<? extends T>>() {
        @Override//from w w  w.  j  a va  2s. c om
        public int compare(Collection<? extends T> list1, Collection<? extends T> list2) {
            return compareAsList(elementComparator, list1, list2);
        }
    };
}

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<Map.Entry<Long, Long>>(set);
    Collections.sort(list, new Comparator<Map.Entry<Long, Long>>() {

        @Override//  www  .java2s  .  c o  m
        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;
}

From source file:Main.java

public static List<Entry<Long, Long>> sortEntrySetToList(Set<Entry<Long, Long>> set) {
    List<Entry<Long, Long>> list = new LinkedList<>(set);
    Collections.sort(list, new Comparator<Entry<Long, Long>>() {

        @Override/*from  w ww .  ja  va  2  s  .c o m*/
        public int compare(Entry<Long, Long> o1, Entry<Long, Long> o2) {
            if (o1.getValue() > o2.getValue()) {
                return 1;
            }
            if (o1.getValue() < o2.getValue()) {
                return -1;
            }
            return 0;
        }
    });
    return list;
}

From source file:Main.java

public static void sortDates(List<DAY> dayList) {
    Collections.sort(dayList, new Comparator<DAY>() {
        public int compare(DAY day1, DAY day2) {
            return day1.getWeight() - day2.getWeight();
        }//from www  .  j ava  2s. co m
    });
    System.out.println("sortedlist is" + dayList.toString());
}

From source file:Main.java

@Nonnull
public static <T> Comparator<Collection<? extends T>> listComparator(
        @Nonnull final Comparator<? super T> elementComparator) {
    return new Comparator<Collection<? extends T>>() {
        @Override//from ww w .  j av  a 2s  .c om
        public int compare(Collection<? extends T> list1, Collection<? extends T> list2) {
            return compareAsList(elementComparator, list1, list2);
        }
    };
}

From source file:Main.java

public static Map<String, Integer> sortByValue(Map<String, Integer> map) {
    List list = new LinkedList(map.entrySet());
    Collections.sort(list, new Comparator() {

        @Override//  w w  w  .jav  a2 s  .  c  o m
        public int compare(Object o1, Object o2) {
            return ((Comparable) ((Map.Entry) (o2)).getValue()).compareTo(((Map.Entry) (o1)).getValue());
        }
    });

    Map result = new LinkedHashMap();
    for (Iterator it = list.iterator(); it.hasNext();) {
        Map.Entry entry = (Map.Entry) it.next();
        result.put(entry.getKey(), entry.getValue());
    }
    return result;
}

From source file:Main.java

public static <K, V extends Comparable<? super V>> SortedSet<Entry<K, V>> sortMapByValueAsc(Map<K, V> map) {
    SortedSet<Entry<K, V>> sorted = new TreeSet<>(new Comparator<Entry<K, V>>() {
        @Override/* w ww. ja va 2  s  .  c  om*/
        public int compare(Entry<K, V> e1, Entry<K, V> e2) {
            int r = e1.getValue().compareTo(e2.getValue());
            return r == 0 ? 1 : r;
        }
    });

    for (Entry<K, V> entry : map.entrySet())
        sorted.add(new SimpleEntry(entry));
    return sorted;
}

From source file:Main.java

public static <K, V extends Comparable<? super V>> SortedSet<Entry<K, V>> sortMapByValueDesc(Map<K, V> map) {
    SortedSet<Entry<K, V>> sorted = new TreeSet<>(new Comparator<Entry<K, V>>() {
        @Override//from   w  ww . j a v a  2 s  .co m
        public int compare(Entry<K, V> e1, Entry<K, V> e2) {
            int r = e2.getValue().compareTo(e1.getValue());
            return r == 0 ? 1 : r;
        }
    });

    for (Entry<K, V> entry : map.entrySet())
        sorted.add(new SimpleEntry(entry));
    return sorted;
}

From source file:Main.java

public static <T> Integer[] getSortedArrayIndexes(final T[] array, final Comparator<T> comparator) {
    Integer[] indexes = new Integer[array.length];
    for (int i = 0; i < indexes.length; i++) {
        indexes[i] = i;//from   ww  w. j  ava  2 s.  c om
    }

    if (comparator != null) {
        Arrays.sort(indexes, new Comparator<Integer>() {
            @Override
            public int compare(Integer aIndex, Integer bIndex) {
                return comparator.compare(array[aIndex], array[bIndex]);
            }
        });
    }

    return indexes;
}

From source file:Main.java

public static void sortFileByDate(List<File> allFileList) {
    File[] files = new File[allFileList.size()];

    for (int i = 0; i < allFileList.size(); i++) {
        files[i] = allFileList.get(i);/*from w  w w .j  ava  2  s . c  o  m*/
    }

    Arrays.sort(files, new Comparator<File>() {
        public int compare(File f1, File f2) {
            return Long.valueOf(f1.lastModified()).compareTo(f2.lastModified());
        }
    });

    allFileList.clear();

    allFileList.addAll(Arrays.asList(files));
}