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

/**
 * Returns the indices that would sort an array.
 * @param array Array./*from   w ww.  j a v a 2 s .c  o m*/
 * @param ascending Ascending order.
 * @return Array of indices.
 */
public static int[] Argsort(final int[] array, final boolean ascending) {
    Integer[] indexes = new Integer[array.length];
    for (int i = 0; i < indexes.length; i++) {
        indexes[i] = i;
    }
    Arrays.sort(indexes, new Comparator<Integer>() {
        @Override
        public int compare(final Integer i1, final Integer i2) {
            return (ascending ? 1 : -1) * Integer.compare(array[i1], array[i2]);
        }
    });
    return asArray(indexes);
}

From source file:Main.java

public static File[] listBackups(File backupPath) {

    if (!backupPath.isDirectory() || !backupPath.canRead()) {
        return new File[0];
    }//  ww w .  j a v a 2 s  .  c om
    File[] files = backupPath.listFiles(new FilenameFilter() {
        @Override
        public boolean accept(File dir, String filename) {
            return (new File(dir, filename)).isFile() && filename.endsWith(".backup");
        }
    });

    if (files != null) {
        Arrays.sort(files, new Comparator<File>() {
            @Override
            public int compare(File s1, File s2) {
                return s2.getName().compareTo(s1.getName());
            }
        });
        return files;
    } else {
        return new File[0];
    }
}

From source file:Main.java

/**
 * Sort the CopyOnWriteArrayList using the given comparator.
 *
 * @param list       The list to sort./*w  ww  .jav  a  2s .c  om*/
 * @param comparator The comparator to use.
 * @param <E>        The type of data.
 */
public static <E> void sort(CopyOnWriteArrayList<E> list, Comparator<E> comparator) {
    Object[] content = list.toArray();
    Arrays.sort(content, (Comparator) comparator);

    for (int i = 0; i < content.length; i++) {
        list.set(i, (E) content[i]);
    }
}

From source file:com.dnielfe.manager.utils.SortUtils.java

public static ArrayList<String> sortList(ArrayList<String> content, String current) {
    int len = content != null ? content.size() : 0;
    int index = 0;
    String[] items = new String[len];
    content.toArray(items);/*from  w ww  . ja va 2  s .com*/

    switch (Settings.mSortType) {
    case SORT_ALPHA:
        Arrays.sort(items, Comparator_ALPH);
        content.clear();

        for (String a : items) {
            content.add(a);
        }
        break;
    case SORT_SIZE:
        Arrays.sort(items, Comparator_SIZE);
        content.clear();

        for (String a : items) {
            if (new File(current + "/" + a).isDirectory())
                content.add(index++, a);
            else
                content.add((String) a);
        }
        break;
    case SORT_TYPE:
        Arrays.sort(items, Comparator_TYPE);
        content.clear();

        for (String a : items) {
            if (new File(current + "/" + a).isDirectory())
                content.add(index++, a);
            else
                content.add(a);
        }
        break;

    case SORT_DATE:
        Arrays.sort(items, Comparator_DATE);
        content.clear();

        for (String a : items) {
            if (new File(current + "/" + a).isDirectory())
                content.add(index++, a);
            else
                content.add(a);
        }
        break;
    }

    return content;
}

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

/**
 * @param files/*from   w ww . ja v a  2  s  . c  o  m*/
 * @param desc
 * @return
 */
public static File[] sortingByExt(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();
            final int s1Dot = s1.lastIndexOf('.');
            final int s2Dot = s2.lastIndexOf('.');
            //
            if ((s1Dot == -1) == (s2Dot == -1)) { // both or neither
                s1 = s1.substring(s1Dot + 1);
                s2 = s2.substring(s2Dot + 1);
                Integer i1 = Integer.parseInt(s1);
                Integer i2 = Integer.parseInt(s2);
                return i1.compareTo(i2);
            } else if (s1Dot == -1) { // only s2 has an extension, so s1 goes first
                return -1;
            } else { // only s1 has an extension, so s1 goes second
                return 1;
            }
        }
    });
    if (desc) {
        org.apache.commons.lang.ArrayUtils.reverse(files);
    }
    return files;
}

From source file:com.lovejoy777sarootool.rootool.utils.SortUtils.java

public static void sortList(ArrayList<String> content, String current) {
    int len = content != null ? content.size() : 0;
    int index = 0;
    String[] items = new String[len];
    content.toArray(items);/*w  ww. jav a2  s  .  c o m*/

    switch (Settings.mSortType) {
    case SORT_ALPHA:
        Arrays.sort(items, Comparator_ALPH);
        content.clear();

        Collections.addAll(content, items);
        break;
    case SORT_SIZE:
        Arrays.sort(items, Comparator_SIZE);
        content.clear();

        for (String a : items) {
            if (new File(current + "/" + a).isDirectory())
                content.add(index++, a);
            else
                content.add(a);
        }
        break;
    case SORT_TYPE:
        Arrays.sort(items, Comparator_TYPE);
        content.clear();

        for (String a : items) {
            if (new File(current + "/" + a).isDirectory())
                content.add(index++, a);
            else
                content.add(a);
        }
        break;

    case SORT_DATE:
        Arrays.sort(items, Comparator_DATE);
        content.clear();

        for (String a : items) {
            if (new File(current + "/" + a).isDirectory())
                content.add(index++, a);
            else
                content.add(a);
        }
        break;
    }

    if (Settings.reverseListView()) {
        Collections.reverse(content);
    }
}

From source file:Main.java

/**
 * Returns the indices that would sort an array.
 * @param array Array.//from w w  w  . j av a2 s  .  c  o m
 * @param ascending Ascending order.
 * @return Array of indices.
 */
public static int[] Argsort(final float[] array, final boolean ascending) {
    Integer[] indexes = new Integer[array.length];
    for (int i = 0; i < indexes.length; i++) {
        indexes[i] = i;
    }
    Arrays.sort(indexes, new Comparator<Integer>() {
        @Override
        public int compare(final Integer i1, final Integer i2) {
            return (ascending ? 1 : -1) * Float.compare(array[i1], array[i2]);
        }
    });
    return asArray(indexes);
}

From source file:com.opengamma.analytics.financial.interestrate.InterestRateCurveSensitivityUtils.java

/**
 * Takes a list of curve sensitivities (i.e. an unordered list of pairs of times and sensitivities) and returns a list order by ascending
 * time, and with sensitivities that occur at the same time netted (zero net sensitivities are removed)
 * @param old An unordered list of pairs of times and sensitivities
 * @param relTol Relative tolerance - if the net divided by gross sensitivity is less than this it is ignored/removed
 * @param absTol Absolute tolerance  - is the net sensitivity is less than this it is ignored/removed
 * @return A time ordered netted list// w ww.jav  a2s  . c o  m
 */
static final List<DoublesPair> clean(final List<DoublesPair> old, final double relTol, final double absTol) {

    Validate.notNull(old, "null list");
    Validate.isTrue(relTol >= 0.0 && absTol >= 0.0);
    if (old.size() == 0) {
        return new ArrayList<DoublesPair>();
    }
    final List<DoublesPair> res = new ArrayList<DoublesPair>();
    final DoublesPair[] sort = old.toArray(new DoublesPair[] {});
    Arrays.sort(sort, FirstThenSecondDoublesPairComparator.INSTANCE);
    final DoublesPair pairOld = sort[0];
    double tOld = pairOld.first;
    double sum = pairOld.getSecond();
    double scale = Math.abs(sum);
    double t = tOld;
    for (int i = 1; i < sort.length; i++) {
        final DoublesPair pair = sort[i];
        t = pair.first;
        if (t > tOld) {
            if (Math.abs(sum) > absTol && Math.abs(sum) / scale > relTol) {
                res.add(new DoublesPair(tOld, sum));
            }
            tOld = t;
            sum = pair.getSecondDouble();
            scale = Math.abs(sum);
        } else {
            sum += pair.getSecondDouble();
            scale += Math.abs(pair.getSecondDouble());
        }
    }

    if (Math.abs(sum) > absTol && Math.abs(sum) / scale > relTol) {
        res.add(new DoublesPair(t, sum));
    }

    return res;
}

From source file:filterviewplugin.FilterViewSettings.java

private static ProgramFilter[] getAvailableFilters() {
    ProgramFilter[] allFilters = Plugin.getPluginManager().getFilterManager().getAvailableFilters().clone();
    Arrays.sort(allFilters, new Comparator<ProgramFilter>() {

        public int compare(ProgramFilter f1, ProgramFilter f2) {
            return f1.getName().compareToIgnoreCase(f2.getName());
        }/*  w  w w .j  av  a2  s  .c o m*/
    });
    ArrayList<ProgramFilter> filters = new ArrayList<ProgramFilter>(Arrays.asList(allFilters));
    filters.remove(Plugin.getPluginManager().getFilterManager().getAllFilter());
    return filters.toArray(new ProgramFilter[filters.size()]);
}

From source file:com.mac.holdempoker.app.impl.SimpleHandAggregator.java

@Override
public long scoreHand(Hand hand) throws Exception {
    if (Objects.nonNull(hand)) {
        AbstractHand ah = hand.getHand();
        HandType ht = ah.getHandType();/*  w w w .java  2s .co m*/
        Card[] cards = ah.getHand();
        Arrays.sort(cards, this);
        switch (ht) {
        case ROYAL_FLUSH: {
            return ht.getInterValue();
        }
        case STRAIGHT_FLUSH: {
            return ht.getInterValue() + scoreHighHand(cards);
        }
        case FOUR_OF_A_KIND: {
            return ht.getInterValue() + scoreTripsBoatQuads(cards);
        }
        case FULL_HOUSE: {
            return ht.getInterValue() + scoreTripsBoatQuads(cards);
        }
        case FLUSH: {
            return ht.getInterValue() + scoreHighHand(cards);
        }
        case STRAIGHT: {
            return ht.getInterValue() + scoreHighHand(cards);
        }
        case THREE_OF_A_KIND: {
            return ht.getInterValue() + scoreTripsBoatQuads(cards);
        }
        case TWO_PAIR: {
            return ht.getInterValue() + scoreTwoPair(cards);
        }
        case PAIR: {
            return ht.getInterValue() + scorePair(cards);
        }
        case HIGH: {
            return scoreHighHand(cards);
        }
        default: {
            return 0L;
        }
        }
    }
    return 0;
}