Example usage for java.util Collections sort

List of usage examples for java.util Collections sort

Introduction

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

Prototype

@SuppressWarnings({ "unchecked", "rawtypes" })
public static <T> void sort(List<T> list, Comparator<? super T> c) 

Source Link

Document

Sorts the specified list according to the order induced by the specified comparator.

Usage

From source file:Main.java

public static <T extends Comparable> List<Entry<String, T>> sortMapDesc(Map<String, T> keywordMap) {
    List<Entry<String, T>> arrayList = new ArrayList<Entry<String, T>>(keywordMap.entrySet());
    Collections.sort(arrayList, new Comparator<Entry<String, T>>() {

        @Override/*ww w  . ja v a  2 s  . c o m*/
        public int compare(Entry<String, T> e1, Entry<String, T> e2) {
            return (e2.getValue()).compareTo(e1.getValue());
        }
    });
    return arrayList;
}

From source file:Main.java

public static <T> List<T> sort(final Collection<T> collection, final Comparator<T> comparator) {
    final List<T> sorted = new ArrayList<T>(collection);
    if (sorted.size() > 1) {
        Collections.sort(sorted, comparator);
    }/*from w  w w  . j  a v a  2 s  . c o m*/
    return sorted;
}

From source file:Main.java

public static <K, V> Map<K, V> sortMapByKey(Map<K, V> map, final Boolean asc) {
    List<Entry<K, V>> entries = new LinkedList<Entry<K, V>>(map.entrySet());
    Collections.sort(entries, new Comparator<Entry<K, V>>() {
        @SuppressWarnings("unchecked")
        public int compare(Entry<K, V> o1, Entry<K, V> o2) {
            if (asc) {
                return ((Comparable<K>) o1.getKey()).compareTo(o2.getKey());
            } else {
                return -((Comparable<K>) o1.getKey()).compareTo(o2.getKey());
            }/*  ww  w .  jav a2s  .c  o m*/
        }
    });

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

From source file:Main.java

public static <K, V> Map<K, V> sortMapByValue(Map<K, V> map, final Boolean asc) {
    List<Entry<K, V>> entries = new LinkedList<Entry<K, V>>(map.entrySet());
    Collections.sort(entries, new Comparator<Entry<K, V>>() {
        @SuppressWarnings("unchecked")
        public int compare(Entry<K, V> o1, Entry<K, V> o2) {
            if (asc) {
                return ((Comparable<V>) o1.getValue()).compareTo(o2.getValue());
            } else {
                return -((Comparable<V>) o1.getValue()).compareTo(o2.getValue());
            }/*from  w  ww.j  ava  2  s. com*/
        }
    });

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

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.entrySet());
    Collections.sort(list, new Comparator<Map.Entry<K, V>>() {
        public int compare(Map.Entry<K, V> o1, Map.Entry<K, V> o2) {
            return (o1.getValue()).compareTo(o2.getValue());
        }/*w w  w . j a  va 2 s  . c  om*/
    });

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

From source file:Main.java

private static List<ThreadInfo> sort(ThreadInfo[] dumpAllThreads) {
    List<ThreadInfo> allThreads = new ArrayList<ThreadInfo>(Arrays.asList(dumpAllThreads));
    Collections.sort(allThreads, new Comparator<ThreadInfo>() {
        @Override//from   w  ww . j av  a  2 s .  c o  m
        public int compare(ThreadInfo o1, ThreadInfo o2) {
            return o1.getThreadName().compareTo(o2.getThreadName());
        }
    });
    return allThreads;
}

From source file:Main.java

private static List<String> getSortedGPXFilenames(File dir, String sub) {
    final List<String> list = new ArrayList<String>();
    readGpxDirectory(dir, list, "");
    Collections.sort(list, new Comparator<String>() {
        @Override//from   w  ww .  ja  v a2s  .com
        public int compare(String object1, String object2) {
            if (object1.compareTo(object2) > 0) {
                return -1;
            } else if (object1.equals(object2)) {
                return 0;
            }
            return 1;
        }

    });
    return list;
}

From source file:Main.java

public static List<ApplicationInfo> getInstalledApplicationInfoList(Context context) {
    List<ApplicationInfo> applicationInfoList = getPackageManager(context).getInstalledApplications(0);
    Collections.sort(applicationInfoList,
            new ApplicationInfo.DisplayNameComparator(context.getPackageManager()));
    return applicationInfoList;
}

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(Entry<K, V> o1, Entry<K, V> o2) {
            return (o2.getValue()).compareTo(o1.getValue());
        }//from  www.j a  va2  s  . com

    });
    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 String getKeyFromParams(List<NameValuePair> nameValuePairs, String publishId) {
    Collections.sort(nameValuePairs, new Comparator<NameValuePair>() {
        @Override//from   www  . j  a v  a2 s  . co  m
        public int compare(NameValuePair p1, NameValuePair p2) {
            return p1.getName().compareTo(p2.getName());
        }
    });

    StringBuilder keyBuilder = new StringBuilder();
    boolean isFirst = true;
    for (NameValuePair nvp : nameValuePairs) {
        if (!isFirst) {
            keyBuilder.append("&");
        }
        keyBuilder.append(nvp.getName()).append("=").append(nvp.getValue());
        isFirst = false;
    }

    keyBuilder.append("&").append(publishId);

    String key = keyBuilder.toString();
    byte[] keyBytes = getBytes(key);
    return getMd5Digest(new String(Base64.encode(keyBytes, Base64.NO_WRAP)));
}