Example usage for java.util Arrays sort

List of usage examples for java.util Arrays sort

Introduction

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

Prototype

public static <T> void sort(T[] a, Comparator<? super T> c) 

Source Link

Document

Sorts the specified array of objects according to the order induced by the specified comparator.

Usage

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;//from  www.j  ava  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

private static void sortFieldsByName(Field[] array) {
    Arrays.sort(array, new Comparator<Field>() {
        @Override/*  w ww  .  j ava  2  s.  com*/
        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 String[] sortQualities(String[] qualities) {
    Arrays.sort(qualities, new Comparator<String>() {
        @Override//from  w  w w .ja  v a  2 s  .  co  m
        public int compare(String lhs, String rhs) {
            if (lhs.contains("p") && rhs.contains("p")) {
                int q1 = Integer.parseInt(lhs.substring(0, lhs.indexOf('p')));
                int q2 = Integer.parseInt(rhs.substring(0, rhs.indexOf('p')));
                if (q1 < q2) {
                    return 1;
                } else if (q1 < q2) {
                    return -1;
                } else {
                    return 0;
                }
            }

            if (lhs.equals("3D"))
                return 1;

            return 0;
        }
    });
    return qualities;
}

From source file:Main.java

public static <T extends Comparable> Integer[] getSortedArrayIndexes(final T[] array) {
    Integer[] indexes = new Integer[array.length];
    for (int i = 0; i < indexes.length; i++) {
        indexes[i] = i;/*from   ww  w. ja v a 2  s.  co m*/
    }

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

    return indexes;
}

From source file:Main.java

License:asdf

@SuppressWarnings("unchecked")
public static <K, V> Map<K, V> sort(Map<K, V> in, Comparator<? super V> compare) {
    Map<K, V> result = new LinkedHashMap<K, V>();
    V[] array = (V[]) in.values().toArray();
    for (int i = 0; i < array.length; i++) {

    }// w w  w.j a  v a2s  .c  o  m
    Arrays.sort(array, compare);
    for (V item : array) {
        K key = (K) getKey(in, item);
        result.put(key, item);
    }
    return result;
}

From source file:com.yahoo.egads.utilities.AutoSensitivity.java

public static Float getLowDensitySensitivity(Float[] data, float sDAutoSensitivy, float amntAutoSensitivity) {
    Float toReturn = Float.POSITIVE_INFINITY;
    Arrays.sort(data, Collections.reverseOrder());
    while (data.length > 0) {

        ArrayList<Float> fData = new ArrayList<Float>();
        fData.add(data[0]);//w  ww  .  j a  v a  2s.co m
        data = ((Float[]) ArrayUtils.remove(data, 0));

        Float centroid = (float) fData.get(0);
        Float maxDelta = (float) sDAutoSensitivy * StatsUtils.getSD(data, StatsUtils.getMean(data));

        logger.debug("AutoSensitivity: Adding: " + fData.get(0) + " SD: " + maxDelta);

        // Add points while it's in the same cluster or not part of the other cluster.
        String localDebug = null;
        while (data.length > 0 && (centroid - data[0]) <= ((float) (maxDelta))) {
            float maxDeltaInit = maxDelta;
            fData.add(data[0]);
            data = ((Float[]) ArrayUtils.remove(data, 0));
            Float[] tmp = new Float[fData.size()];
            tmp = fData.toArray(tmp);
            centroid = StatsUtils.getMean(tmp);

            if (data.length > 0) {
                Float sdOtherCluster = (float) StatsUtils.getSD(data, StatsUtils.getMean(data));
                maxDelta = sDAutoSensitivy * sdOtherCluster;
                logger.debug(
                        "AutoSensitivity: Adding: " + data[0] + " SD: " + maxDeltaInit + " SD': " + maxDelta);
            }
        }
        if (data.length > 0) {
            logger.debug("AutoSensitivity: Next Point I would have added is " + data[0]);
        }

        if (((double) fData.size() / (double) data.length) > amntAutoSensitivity) {
            // Cannot do anomaly detection.
            logger.debug("AutoSensitivity: Returning " + toReturn + " data size: " + data.length
                    + " fData.size: " + fData.size());
            return toReturn;
        }

        toReturn = fData.get(fData.size() - 1);
        logger.debug("AutoSensitivity: Updating toReturn:  " + toReturn + " SD: " + maxDelta);
        return toReturn;
    }
    return toReturn;
}

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

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

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

/**
 * @param files//from  w  w w. j  a va  2s  . c  om
 * @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

/**
 * /*w  ww .j  a va 2s .c  o  m*/
 * @param <T>
 * @param comparator
 * @param a
 * @return
 */
public static <T> T getLowerMiddleValue(Comparator<T> comparator, T... a) {
    if (null == a || a.length <= 0)
        return null;
    Arrays.sort(a, comparator);
    return a[(a.length - 1) / 2];
}

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++)
        ;//w ww.ja v  a2 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);
}