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:com.gson.util.Tools.java

public static final boolean checkSignature(String token, String signature, String timestamp, String nonce) {
    List<String> params = new ArrayList<String>();
    params.add(token);//ww  w. j av a 2 s . c  o m
    params.add(timestamp);
    params.add(nonce);
    Collections.sort(params, new Comparator<String>() {
        @Override
        public int compare(String o1, String o2) {
            return o1.compareTo(o2);
        }
    });
    String temp = params.get(0) + params.get(1) + params.get(2);
    return DigestUtils.shaHex(temp).equals(signature);
}

From source file:com.hangum.tadpole.commons.util.IPUtil.java

/**
 * ip sort//w w  w  .jav a  2 s  .  c om
 * 
 * @param ipList
 * @return
 */
public static List<String> ipSort(List<String> ipList) {

    Collections.sort(ipList, new Comparator<String>() {
        @Override
        public int compare(String o1, String o2) {
            String[] ips1 = o1.split("\\.");
            String updatedIp1 = String.format("%3s.%3s.%3s.%3s", ips1[0], ips1[1], ips1[2], ips1[3]);
            String[] ips2 = o2.split("\\.");
            String updatedIp2 = String.format("%3s.%3s.%3s.%3s", ips2[0], ips2[1], ips2[2], ips2[3]);
            return updatedIp1.compareTo(updatedIp2);
        }
    });

    return ipList;
}

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

/**
 * @param files/*  w  w  w  .  j  av  a  2s.  co m*/
 * @param desc
 * @return
 */
public static File[] sortingByName(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();
            return s1.compareTo(s2);
        }
    });
    if (desc) {
        org.apache.commons.lang.ArrayUtils.reverse(files);
    }
    return files;
}

From source file:Main.java

/**
 * @return a sorted list where the object with the highest integer value comes first!
 *///from  w w  w  .  j  a  v  a  2 s . com
public static <T> List<Entry<T, Integer>> sort(Collection<Entry<T, Integer>> entrySet) {
    List<Entry<T, Integer>> sorted = new ArrayList<Entry<T, Integer>>(entrySet);
    Collections.sort(sorted, new Comparator<Entry<T, Integer>>() {
        @Override
        public int compare(Entry<T, Integer> o1, Entry<T, Integer> o2) {
            int i1 = o1.getValue();
            int i2 = o2.getValue();
            if (i1 < i2) {
                return 1;
            } else if (i1 > i2) {
                return -1;
            } else {
                return 0;
            }
        }
    });

    return sorted;
}

From source file:Main.java

/**
 * @return a sorted list where the object with the highest integer value comes first!
 *//*from ww  w  . ja va  2  s  .  c o  m*/
public static <T> List<Entry<T, Integer>> sort(Collection<Entry<T, Integer>> entrySet) {
    List<Entry<T, Integer>> sorted = new ArrayList<Entry<T, Integer>>(entrySet);
    Collections.sort(sorted, new Comparator<Entry<T, Integer>>() {

        @Override
        public int compare(Entry<T, Integer> o1, Entry<T, Integer> o2) {
            int i1 = o1.getValue();
            int i2 = o2.getValue();
            if (i1 < i2)
                return 1;
            else if (i1 > i2)
                return -1;
            else
                return 0;
        }
    });

    return sorted;
}

From source file:Main.java

/**
 * @return a sorted list where the string with the highest integer value comes first!
 *//*from   w w w  . j a  v a2s. co m*/
public static <T> List<Entry<T, Long>> sortLong(Collection<Entry<T, Long>> entrySet) {
    List<Entry<T, Long>> sorted = new ArrayList<Entry<T, Long>>(entrySet);
    Collections.sort(sorted, new Comparator<Entry<T, Long>>() {
        @Override
        public int compare(Entry<T, Long> o1, Entry<T, Long> o2) {
            long i1 = o1.getValue();
            long i2 = o2.getValue();
            if (i1 < i2) {
                return 1;
            } else if (i1 > i2) {
                return -1;
            } else {
                return 0;
            }
        }
    });

    return sorted;
}

From source file:Main.java

/**
 * @return a sorted list where the string with the highest integer value comes first!
 *//*from  w w  w .j  av  a 2 s.co m*/
public static <T> List<Entry<T, Long>> sortLong(Collection<Entry<T, Long>> entrySet) {
    List<Entry<T, Long>> sorted = new ArrayList<Entry<T, Long>>(entrySet);
    Collections.sort(sorted, new Comparator<Entry<T, Long>>() {

        @Override
        public int compare(Entry<T, Long> o1, Entry<T, Long> o2) {
            long i1 = o1.getValue();
            long i2 = o2.getValue();
            if (i1 < i2)
                return 1;
            else if (i1 > i2)
                return -1;
            else
                return 0;
        }
    });

    return sorted;
}

From source file:com.github.jinahya.sql.database.metadata.bind.ImportedKey.java

public static Comparator<ImportedKey> natural() {

    return new Comparator<ImportedKey>() {

        @Override//from  w w w.j av a  2 s.c om
        public int compare(final ImportedKey o1, final ImportedKey o2) {

            // by PKTABLE_CAT, PKTABLE_SCHEM, PKTABLE_NAME, and KEY_SEQ.
            return new CompareToBuilder().append(o1.getPktableCat(), o2.getPktableCat())
                    .append(o1.getPktableSchem(), o2.getPktableSchem())
                    .append(o1.getPktableName(), o2.getPktableName()).append(o1.getKeySeq(), o2.getKeySeq())
                    .build();
        }

    };
}

From source file:de.tudarmstadt.lt.utilities.ArrayUtils.java

public static <T> int[] sortIdsByValue(final T[] values, final Comparator<T> comparator) {
    Integer[] ids = new Integer[values.length];
    for (int id = 0; id < ids.length; ids[id] = id++)
        ;/*from  ww w.  j  a va  2 s.  c o m*/
    Arrays.sort(ids, new Comparator<Integer>() {
        @Override
        public int compare(Integer o1, Integer o2) {
            return comparator.compare(values[o1], values[o2]);
        }
    });
    return org.apache.commons.lang.ArrayUtils.toPrimitive(ids);
}

From source file:com.github.jinahya.sql.database.metadata.bind.TableType.java

public static Comparator<TableType> natural() {

    return new Comparator<TableType>() {

        @Override/*ww w .  j a va 2s .  c  o  m*/
        public int compare(final TableType o1, final TableType o2) {

            // by table type
            return new CompareToBuilder().append(o1.getTableType(), o2.getTableType()).build();
        }

    };
}