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:edu.unc.lib.dl.services.BatchIngestQueue.java

public File[] getFailedDirectories() {
    File[] batchDirs = this.failedDirectory.listFiles(new FileFilter() {
        @Override//from  w w w  .jav a 2s.  c  o m
        public boolean accept(File arg0) {
            return arg0.isDirectory();
        }
    });
    if (batchDirs != null) {
        Arrays.sort(batchDirs, new Comparator<File>() {
            @Override
            public int compare(File o1, File o2) {
                if (o1 == null || o2 == null)
                    return 0;
                return (int) (o1.lastModified() - o2.lastModified());
            }
        });
        return batchDirs;
    } else {
        return new File[] {};
    }
}

From source file:hudson.logging.LogRecorder.java

@Restricted(NoExternalUse.class)
Target[] orderedTargets() {/*from   w w  w  .  j  a  va 2 s.  co m*/
    // will contain targets ordered by reverse name length (place specific targets at the beginning)
    Target[] ts = targets.toArray(new Target[] {});

    Arrays.sort(ts, TARGET_COMPARATOR);

    return ts;
}

From source file:controller.VisLP.java

/**
 * @param  points//from  w w  w  .j a  v  a 2 s .  c om
 *         Unordered list of points that can form a
 *         convex polygon, but in the given order
 *         does not necessarily form a convex
 *         polygon if edges are drawn between the
 *         points in the given order.
 * @return 
 *         An ordered list of points that when edges
 *         are drawn in this order is guaranteed to
 *         form a convex polygon.
 */
static Point2D[] convex(Point2D[] points) {
    /* 
     * Sort the points first on x-value then
     * on y-value, both in ascending order.
     */
    Arrays.sort(points, new Comparator<Point2D>() {
        @Override
        public int compare(Point2D o1, Point2D o2) {
            double s = o1.getX() - o2.getX();
            if (s > 0)
                return 1;
            if (s < 0)
                return -1;

            s = o1.getY() - o2.getY();
            if (s > 0)
                return 1;
            if (s < 0)
                return -1;

            return 0;
        }
    });

    Point2D x_min = points[0];
    Point2D x_max = points[points.length - 1];

    ArrayList<Point2D> upper = new ArrayList<Point2D>();
    ArrayList<Point2D> lower = new ArrayList<Point2D>();

    upper.add(x_min);

    /* Find the slope of the line L connecting x_min and x_max */
    double mx = x_max.getX() - x_min.getX();
    double my = x_max.getY() - x_min.getY();
    double m = my / mx;

    /* Intersection of y-axis */
    double b = x_max.getY() - (m * x_max.getX());

    /* Add points above/below L to upper/lower, respectively */
    for (int i = 1; i < points.length - 1; i++) {
        Point2D p2d = points[i];
        double y = p2d.getX() * m + b;

        if (p2d.getY() >= y)
            upper.add(p2d);
        else
            lower.add(p2d);
    }

    /* Sort the lower list in descending order */
    lower.add(x_max);
    Collections.reverse(lower);

    upper.addAll(lower);
    return upper.toArray(new Point2D[0]);
}