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 void main(String[] args) {
    int[][] array2d = { { 21, 14, 13, 12, 15 }, { 4, 25, 23, 22, 9 }, { 4, 7, 8, 98, 24 } };
    int[] list = new int[array2d.length * array2d[0].length];

    int listPos = 0;
    for (int i = 0; i < array2d.length; i++) {
        for (int j = 0; j < array2d.length; j++) {
            list[listPos++] = array2d[i][j];
        }//  ww  w .  ja  v a 2  s  .co m
    }
    Arrays.sort(list);
    System.out.println(median(list));
}

From source file:Main.java

public static void main(String[] args) throws FileNotFoundException {
    List<Integer> arraySource = new ArrayList<>();
    for (int i = 0; i < 99999; i++) {
        arraySource.add(i);// w w w.  j av  a  2s.c om
    }

    Integer[] myArray = new Integer[999];
    myArray = arraySource.toArray(myArray);
    long startTime = System.currentTimeMillis();
    Arrays.sort(myArray);
    long endTime = System.currentTimeMillis();
    System.out.println("Time take in serial: " + (endTime - startTime) / 1000.0);

    Integer[] myArray2 = new Integer[999];
    myArray2 = arraySource.toArray(myArray);
    startTime = System.currentTimeMillis();
    Arrays.parallelSort(myArray2);
    endTime = System.currentTimeMillis();
    System.out.println("Time take in parallel: " + (endTime - startTime) / 1000.0);

}

From source file:Main.java

public static void main(String[] args) {
    Car car1 = new Car("A", 5000);
    Car car2 = new Car("B", 5000);
    Car car3 = new Car("C", 4000);

    System.out.println("Car 1 equals Car 2: " + car1.compareTo(car2));
    System.out.println("Car 1 equals Car 3: " + car1.compareTo(car3));
    System.out.println("Car 2 equals Car 3: " + car2.compareTo(car3));

    Car[] carArray = new Car[] { car1, car2, car3 };
    Arrays.sort(carArray);

    for (Car car : carArray)
        System.out.println(car.toString());
}

From source file:MainClass.java

public static void main(String args[]) {

    int array[] = new int[10];
    for (int i = 0; i < 10; i++)
        array[i] = 3 * i;/* w  w  w  . ja va2 s .c o  m*/

    System.out.print("Original contents: ");
    display(array);
    Arrays.sort(array);
    System.out.print("Sorted: ");
    display(array);

    Arrays.fill(array, 2, 6, -1);
    System.out.print("After fill(): ");
    display(array);

    Arrays.sort(array);
    System.out.print("After sorting again: ");
    display(array);

    System.out.print("The value 9 is at location ");
    int index = Arrays.binarySearch(array, 9);

    System.out.println(index);
}

From source file:MainClass.java

public static void main(String[] args) {
    // Build a vector of words to be sorted
    ArrayList list = new ArrayList();
    list.add("m");
    list.add("c2");
    list.add("e");
    list.add("c1");

    Collator collate = Collator.getInstance();

    CollationKey[] keys = new CollationKey[list.size()];

    for (int k = 0; k < list.size(); k++)
        keys[k] = collate.getCollationKey((String) list.get(k));

    Arrays.sort(keys);

    for (int l = 0; l < keys.length; l++) {
        System.out.println(keys[l].getSourceString());
    }//from  w  w w.j  ava 2s  . c  o  m
}

From source file:Main.java

public static void main(String[] args) {
    HashMap<String, Integer> map = new HashMap<String, Integer>();
    map.put("d", 5);
    map.put("c", 4);
    map.put("b", 2);
    map.put("a", 1);

    Integer value[] = new Integer[map.size()];

    Set keySet = map.keySet();//from   w  w  w.j ava 2 s.  c  om
    Iterator t = keySet.iterator();
    int a = 0;
    while (t.hasNext()) {
        value[a] = map.get(t.next());
        a++;
    }

    Arrays.sort(value);

    for (int i = 0; i < map.size(); i++) {
        t = keySet.iterator();
        while (t.hasNext()) {
            String temp = (String) t.next();
            if (value[i].equals(map.get(temp))) {
                System.out.println(value[i] + " = " + temp);
            }
        }
    }
}

From source file:MainClass.java

public static void main(final String[] args) {
    File path = new File(".");
    String[] list;//from  w ww.  j a v a2  s.co  m
    if (args.length == 0)
        list = path.list();
    else
        list = path.list(new FilenameFilter() {
            private Pattern pattern = Pattern.compile(args[0]);

            public boolean accept(File dir, String name) {
                return pattern.matcher(new File(name).getName()).matches();
            }
        });
    Arrays.sort(list);
    for (int i = 0; i < list.length; i++)
        System.out.println(list[i]);
}

From source file:ComboSortList.java

public static void main(String[] args) {
    Display display = new Display();
    Shell shell = new Shell(display);
    shell.setLayout(new FillLayout());

    String[] ITEMS = { "A", "B", "C", "D", "E", "F" };
    Arrays.sort(ITEMS);
    final Combo combo = new Combo(shell, SWT.DROP_DOWN);
    combo.setItems(ITEMS);//from w  ww.  j  a v a  2  s  .c o m

    combo.addSelectionListener(new SelectionListener() {
        public void widgetSelected(SelectionEvent e) {
            System.out.println("Selected index: " + combo.getSelectionIndex() + ", selected item: "
                    + combo.getItem(combo.getSelectionIndex()) + ", text content in the text field: "
                    + combo.getText());
        }

        public void widgetDefaultSelected(SelectionEvent e) {
            System.out.println("Default selected index: " + combo.getSelectionIndex() + ", selected item: "
                    + (combo.getSelectionIndex() == -1 ? "<null>" : combo.getItem(combo.getSelectionIndex()))
                    + ", text content in the text field: " + combo.getText());
            String text = combo.getText();
            if (combo.indexOf(text) < 0) { // Not in the list yet.
                combo.add(text);

                // Re-sort
                String[] items = combo.getItems();
                Arrays.sort(items);
                combo.setItems(items);
            }
        }
    });

    shell.open();
    while (!shell.isDisposed()) {
        if (!display.readAndDispatch()) {
            display.sleep();
        }
    }
    display.dispose();
}

From source file:Main.java

  public static void main(String[] args) {
  char charArray[] = { 'a', 'b', 'd', 'e' };
  Arrays.sort(charArray);

  char searchValue = 'b';
  System.out.println(Arrays.binarySearch(charArray, searchValue));

  searchValue = 'z';
  System.out.println(Arrays.binarySearch(charArray, searchValue));

}

From source file:Main.java

public static void main(String[] args) {
    String[] allTimeZones = TimeZone.getAvailableIDs();

    Arrays.sort(allTimeZones);

    for (String timezone : allTimeZones) {
        System.out.println(timezone);
    }//from ww  w.j a v  a 2  s .  c  o  m
}