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 void sort(Object[] a) 

Source Link

Document

Sorts the specified array of objects into ascending order, according to the Comparable natural ordering of its elements.

Usage

From source file:Main.java

public static List convertMapToList(Map map) {
    List list = new ArrayList();

    Object[] o = ((HashMap) map).keySet().toArray();

    Arrays.sort(o);

    for (int i = 0; i <= o.length - 1; i++) {

        if (map.containsKey(String.valueOf(o[i]))) {

            list.add(map.get(String.valueOf(o[i])));

        } else {// w  ww . j  a v a 2 s  .  co  m
            list.add(map.get(""));
        }
    }
    return list;
}

From source file:Main.java

public static int[] getRandomArray(int length, int max) {
    int[] randArray = new int[length];
    Random rand = new Random();
    for (int i = 0; i < length; i++) {
        randArray[i] = rand.nextInt(max);
    }/*from w  w  w .java  2s  .c o m*/
    Arrays.sort(randArray);
    for (int i = 1; i < length; i++) {
        if (randArray[i] == randArray[i - 1]) {
            randArray[i] += 1;
        }
    }
    for (int i = length - 1; i > 0; i--) {
        if (randArray[i] == randArray[i - 1]) {
            randArray[i - 1] -= 1;
        }
    }
    return randArray;

}

From source file:Main.java

private final static String sortedParams(Map<String, String> params) throws UnsupportedEncodingException {
    String vals[] = new String[params.size()];
    int idx = 0;//from  w ww  .  ja  v a2 s. com
    for (String key : params.keySet()) {
        vals[idx++] = key;
    }
    Arrays.sort(vals);
    StringBuilder sb = new StringBuilder();
    for (int i = 0; i < vals.length; i++) {
        if (i > 0) {
            sb.append("&");
        }
        sb.append(vals[i]);
        sb.append("=");
        sb.append(encode(params.get(vals[i])));
    }
    return sb.toString();
}

From source file:Main.java

public static int[] mergeSort(int[] source1, int[] source2) {
    int count1 = source1.length;
    int count2 = source2.length;
    int count = count1 + count2;
    int array[] = new int[count];
    for (int i = 0; i < count; i++) {
        if (i < count1) {
            array[i] = source1[i];//from   w w  w  .  ja  v a2s .co  m
        } else {
            array[i] = source2[i - count1];
        }
    }
    Arrays.sort(array);
    return array;
}

From source file:Main.java

public static int[] divide(int number, int number_of_parts) {
    HashSet<Integer> uniqueInts = new HashSet<Integer>();
    uniqueInts.add(0);//from w  w  w. jav  a2s.  com
    uniqueInts.add(number);
    int array_size = number_of_parts + 1;
    while (uniqueInts.size() < array_size) {
        uniqueInts.add(1 + r.nextInt(number - 1));
    }
    Integer[] dividers = uniqueInts.toArray(new Integer[array_size]);
    Arrays.sort(dividers);
    int[] results = new int[number_of_parts];
    for (int i = 1, j = 0; i < dividers.length; ++i, ++j) {
        results[j] = dividers[i] - dividers[j];
    }
    return results;
}

From source file:Main.java

public static Enumeration getAllFilesIn(File dir) {
    File[] files;//from  w w  w.  ja  v  a2 s.  com
    if (dir.isDirectory()) {
        files = dir.listFiles(new FileFilter() {
            public boolean accept(File f) {
                if (f.isDirectory())
                    return false;
                return (f.getName().endsWith(".txt"));
            }
        });
        Arrays.sort(files);
    } else {
        files = new File[] { dir };
    }
    Vector vect = new Vector(files.length);
    for (int i = 0; i < files.length; ++i)
        vect.addElement(files[i]);
    return vect.elements();
}

From source file:Main.java

public static int[] asSortedArray(int[] a) {
    Arrays.sort(a);
    return a;
}

From source file:Main.java

/**
 * Shift objects in the list, objects will try to move to the furthest distance.
 * <pre>/*from   ww w  .  j  a va2  s.c  o m*/
 * e.g. ABCED -> Shift AC with offset=5 -> BEDAC
 * </pre>
 *
 * @param list       list
 * @param indexArray object index array
 * @param offset     offset value can be positive or negative
 */
public static void shiftItemsAsMuchAsPossible(List<?> list, int[] indexArray, int offset) {
    Arrays.sort(indexArray);
    if (offset > 0) {
        int last = list.size();
        for (int i = indexArray.length - 1; i >= 0; i--) {
            last = shiftItem(list.subList(0, last), indexArray[i], offset);
        }
    } else {
        int last = -1;
        for (int i = 0; i < indexArray.length; i++) {
            int index = indexArray[i] - (last + 1);
            last = shiftItem(list.subList(last + 1, list.size()), index, offset);
        }
    }
}

From source file:Main.java

public static String[] sortNames(String[] names) {
    Arrays.sort(names);
    return names;
}

From source file:Main.java

/**
 * Shift objects in the list, objects will keep the distance between them.
 * <pre>/*from   w  w w .j  a v a  2s. co  m*/
 * e.g. ABCED -> Shift AC with offset=5 -> BEADC
 * </pre>
 *
 * @param list       list
 * @param indexArray object index array
 * @param offset     offset value can be positive or negative
 */
public static void shiftItemsWithFixedDistance(List<?> list, int[] indexArray, int offset) {
    Arrays.sort(indexArray);
    int unionOffset = computeUnionOffset(list, indexArray, offset);
    shiftItemsDirectly(list, indexArray, unionOffset);
}