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

public static void chineseSort(String[] strings) {
    Arrays.sort(strings, Collator.getInstance(java.util.Locale.CHINA));
}

From source file:Main.java

public static File[] dirListByDescendingDate(File[] files) {
    Arrays.sort(files, new Comparator() {
        public int compare(final Object o1, final Object o2) {
            return new Long(((File) o2).lastModified()).compareTo(new Long(((File) o1).lastModified()));
        }/*w w  w .j ava  2  s. c om*/
    });
    return files;
}

From source file:Main.java

public static String[] randomizeArray(String[] array) {

    Arrays.sort(array, new Comparator<String>() {

        private final Random generator = new Random();

        @Override//w w  w  .  j  a v a  2 s  . c  o m
        public int compare(String lhs, String rhs) {
            // returns a random int between 1 and -1
            return (generator.nextInt(1) * 2) - 1;
        }
    });
    return array;
}

From source file:Main.java

private static void reverseWithLambda(Integer[] numbers) {
    Arrays.sort(numbers, (Integer i1, Integer i2) -> {
        return (i2 - i1);
    });//w  w  w .jav a 2 s .  c  o m
}

From source file:Main.java

public static <T> Integer[] getSortedArrayIndexes(final T[] array, final Comparator<T> comparator) {
    Integer[] indexes = new Integer[array.length];
    for (int i = 0; i < indexes.length; i++) {
        indexes[i] = i;//  w  ww . j ava  2s . c  om
    }

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

    return indexes;
}

From source file:Main.java

public static void sortFileByDate(List<File> allFileList) {
    File[] files = new File[allFileList.size()];

    for (int i = 0; i < allFileList.size(); i++) {
        files[i] = allFileList.get(i);/*w w  w  . j a va  2s  .co m*/
    }

    Arrays.sort(files, new Comparator<File>() {
        public int compare(File f1, File f2) {
            return Long.valueOf(f1.lastModified()).compareTo(f2.lastModified());
        }
    });

    allFileList.clear();

    allFileList.addAll(Arrays.asList(files));
}

From source file:Main.java

public static List<Message> getMessagesWithPage(Folder folder, int pageNum, int pageSize)
        throws MessagingException {
    Message[] messages = folder.getMessages();
    Arrays.sort(messages, new Comparator<Message>() {

        public int compare(Message o1, Message o2) {
            Message m1 = (Message) o1;/*  w  w w. j a  v a 2s .  c o m*/
            Message m2 = (Message) o2;
            if (m1.getMessageNumber() > m2.getMessageNumber()) {
                return -1;
            }
            return 1;
        }

    });
    List<Message> list = Arrays.asList(messages);
    int start = (pageNum - 1) * pageSize;
    int end = pageNum * pageSize;
    return list.subList(start, end);
}

From source file:Main.java

public static File[] listFiles(File f) {
    File[] list;/*from  w  ww .j  av  a 2 s  .  c om*/
    if (f.isDirectory()) {
        list = f.listFiles(filter);
        if (list != null) {
            Arrays.sort(list, comparator);
        }
    } else {
        list = new File[] {};
    }
    return list;
}

From source file:Main.java

public static <T> void sort(T[] list, Comparator<T> comp) {
    Arrays.sort(list, comp);
}

From source file:Main.java

/**
 * Returns the indices that would sort an array.
 * @param array Array./*from  w w w. j  ava  2 s .  c o  m*/
 * @param ascending Ascending order.
 * @return Array of indices.
 */
public static int[] Argsort(final double[] 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) * Double.compare(array[i1], array[i2]);
        }
    });
    return asArray(indexes);
}