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.envision.envservice.common.util.UserUtil.java

public static void sortByName(List<UserBo> users) {
    Collections.sort(users, new Comparator<UserBo>() {

        @Override/*from   www.ja  v a2  s .c  o m*/
        public int compare(UserBo u1, UserBo u2) {
            if (StringUtils.isEmpty(u1.getName())) {
                return 1;
            }
            if (StringUtils.isEmpty(u2.getName())) {
                return -1;
            }

            String u1Name = u1.getName();
            if (u1Name.contains(".")) {
                String[] names = u1Name.split("\\.");
                u1Name = names[1] + names[0];
            }

            String u2Name = u2.getName();
            if (u2Name.contains(".")) {
                String[] names = u2Name.split("\\.");
                u2Name = names[1] + names[0];
            }

            return u1Name.compareTo(u2Name);
        }
    });
}

From source file:Main.java

/**
 * Return a List containing all the elements of the specified Iterable that compare as being
 * equal to the maximum element.//from   ww  w  .j a v a  2s.c om
 *
 * @throws NoSuchElementException if the Iterable is empty.
 */
public static <T extends Comparable<? super T>> List<T> maxList(Iterable<T> iterable) {
    return maxList(iterable, new Comparator<T>() {
        public int compare(T o1, T o2) {
            return o1.compareTo(o2);
        }
    });
}

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

public static Comparator<SchemaName> natural() {

    return new Comparator<SchemaName>() {

        @Override//from w w  w  .j ava  2  s .co  m
        public int compare(final SchemaName o1, final SchemaName o2) {

            // by TABLE_CATALOG and TABLE_SCHEM
            return new CompareToBuilder().append(o1.getTableCatalog(), o2.getTableCatalog())
                    .append(o1.getTableSchem(), o2.getTableSchem()).build();
        }

    };
}

From source file:Main.java

/**
 * Returns the indices that would sort an array.
 * @param array Array./*w  w w  . j a v  a  2  s .  co 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:com.github.jinahya.sql.database.metadata.bind.ExportedKey.java

public static Comparator<ExportedKey> natural() {
    return new Comparator<ExportedKey>() {

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

            // by FKTABLE_CAT, FKTABLE_SCHEM, FKTABLE_NAME, and KEY_SEQ.
            return new CompareToBuilder().append(o1.getFktableCat(), o2.getFktableCat())
                    .append(o1.getFktableSchem(), o2.getFktableSchem())
                    .append(o1.getFktableName(), o2.getFktableName()).append(o1.getKeySeq(), o2.getKeySeq())
                    .build();
        }

    };
}

From source file:Main.java

public static File[] listBackups(File backupPath) {

    if (!backupPath.isDirectory() || !backupPath.canRead()) {
        return new File[0];
    }/* w ww. j a va  2 s .  c o m*/
    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

public static <T extends Comparable<? super T>> Comparator<T> createComparatorFromComparable(T c) {
    return new Comparator<T>() {

        public int compare(T s, T t) {
            return s.compareTo(t);
        }//from w  w w. jav a2  s. c om

    };
}

From source file:Main.java

/** Returns an alphabetically sorted copy of vector. */
public static void sortListAlphabetically(List list) {
    Collections.sort(list, new Comparator() {
        public int compare(Object a, Object b) {
            String textA = a.toString();
            String textB = b.toString();

            return textA.compareToIgnoreCase(textB);
        }// w w w  .  j  a  va  2s .  co  m
    });
}

From source file:Main.java

public static Point findBestPreviewSizeValue(Camera.Parameters parameters, Point screenResolution) {

    List<Camera.Size> rawSupportedSizes = parameters.getSupportedPreviewSizes();
    if (rawSupportedSizes == null) {
        Log.w(TAG, "Device returned no supported preview sizes; using default");
        Camera.Size defaultSize = parameters.getPreviewSize();
        if (defaultSize == null) {
            throw new IllegalStateException("Parameters contained no preview size!");
        }//  w  w  w . j ava  2s.  com
        return new Point(defaultSize.width, defaultSize.height);
    }

    // Sort by size, descending
    List<Camera.Size> supportedPreviewSizes = new ArrayList<Camera.Size>(rawSupportedSizes);
    Collections.sort(supportedPreviewSizes, new Comparator<Camera.Size>() {
        @Override
        public int compare(Camera.Size a, Camera.Size b) {
            int aPixels = a.height * a.width;
            int bPixels = b.height * b.width;
            if (bPixels < aPixels) {
                return -1;
            }
            if (bPixels > aPixels) {
                return 1;
            }
            return 0;
        }
    });

    if (Log.isLoggable(TAG, Log.INFO)) {
        StringBuilder previewSizesString = new StringBuilder();
        for (Camera.Size supportedPreviewSize : supportedPreviewSizes) {
            previewSizesString.append(supportedPreviewSize.width).append('x')
                    .append(supportedPreviewSize.height).append(' ');
        }
        Log.i(TAG, "Supported preview sizes: " + previewSizesString);
    }

    double screenAspectRatio = (double) screenResolution.x / (double) screenResolution.y;

    // Remove sizes that are unsuitable
    Iterator<Camera.Size> it = supportedPreviewSizes.iterator();
    while (it.hasNext()) {
        Camera.Size supportedPreviewSize = it.next();
        int realWidth = supportedPreviewSize.width;
        int realHeight = supportedPreviewSize.height;
        if (realWidth * realHeight < MIN_PREVIEW_PIXELS) {
            it.remove();
            continue;
        }

        boolean isCandidatePortrait = realWidth < realHeight;
        int maybeFlippedWidth = isCandidatePortrait ? realHeight : realWidth;
        int maybeFlippedHeight = isCandidatePortrait ? realWidth : realHeight;
        double aspectRatio = (double) maybeFlippedWidth / (double) maybeFlippedHeight;
        double distortion = Math.abs(aspectRatio - screenAspectRatio);
        if (distortion > MAX_ASPECT_DISTORTION) {
            it.remove();
            continue;
        }

        if (maybeFlippedWidth == screenResolution.x && maybeFlippedHeight == screenResolution.y) {
            Point exactPoint = new Point(realWidth, realHeight);
            Log.i(TAG, "Found preview size exactly matching screen size: " + exactPoint);
            return exactPoint;
        }
    }

    // If no exact match, use largest preview size. This was not a great
    // idea on older devices because
    // of the additional computation needed. We're likely to get here on
    // newer Android 4+ devices, where
    // the CPU is much more powerful.
    if (!supportedPreviewSizes.isEmpty()) {
        Camera.Size largestPreview = supportedPreviewSizes.get(0);
        Point largestSize = new Point(largestPreview.width, largestPreview.height);
        Log.i(TAG, "Using largest suitable preview size: " + largestSize);
        return largestSize;
    }

    // If there is nothing at all suitable, return current preview size
    Camera.Size defaultPreview = parameters.getPreviewSize();
    if (defaultPreview == null) {
        throw new IllegalStateException("Parameters contained no preview size!");
    }
    Point defaultSize = new Point(defaultPreview.width, defaultPreview.height);
    Log.i(TAG, "No suitable preview sizes, using default: " + defaultSize);
    return defaultSize;
}

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

public static Comparator<ClientInfoProperty> natural() {

    return new Comparator<ClientInfoProperty>() {

        @Override//from   ww  w.  ja v a 2s  .com
        public int compare(final ClientInfoProperty o1, final ClientInfoProperty o2) {

            // by the NAME column
            return new CompareToBuilder().append(o1.getName(), o2.getName()).build();

        }

    };
}