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:ComparatorExampleForUserDefinedClass.java

public static void main(String args[]) {
    prepareData();// www . j a v a 2 s  .c  om

    ComparatorChain chain = new ComparatorChain();
    chain.addComparator(new NameComparator());
    chain.addComparator(new NumberComparator());

    printArray(dataArray);

    Arrays.sort(dataArray, chain);

    printArray(dataArray);
}

From source file:Compare2DArray.java

public static void main(String args[]) {
    int d2[][] = { { 11, 43 }, { 6, 98 }, { 44, 38 }, { 11, 4 }, { 17, 32 } };
    Arrays.sort(d2, new Compare2DArray());
    for (int i = 0; i < d2.length; i++) {
        for (int j = 0; j < d2[i].length; j++)
            System.out.print(d2[i][j] + " ");
        System.out.println();/*ww  w. j a v a  2s. c  o  m*/
    }
}

From source file:CompTypeComparator.java

public static void main(String[] args) {
    Integer[] a = new Integer[10];
    for (int i = 0; i < a.length; i++) {
        a[i] = i;//from   ww  w. jav a2s.  c  o  m
    }

    System.out.println("before sorting, a = " + Arrays.asList(a));
    Arrays.sort(a, new CompTypeComparator());
    System.out.println("after sorting, a = " + Arrays.asList(a));
}

From source file:DirList.java

public static void main(String[] args) {
    File path = new File(".");
    String[] list;/*  w w w .j av a 2  s .c  o  m*/
    if (args.length == 0)
        list = path.list();
    else
        list = path.list(new DirFilter(args[0]));
    Arrays.sort(list, new AlphabeticComparator());
    for (int i = 0; i < list.length; i++)
        System.out.println(list[i]);
}

From source file:Main.java

public static void main(String[] argv) throws Exception {
    DefaultTableModel model = new DefaultTableModel();
    JTable table = new JTable(model);
    table.setAutoCreateColumnsFromModel(false);

    boolean ascending = false;
    Vector data = model.getDataVector();
    Object[] colData = new Object[model.getRowCount()];

    for (int i = 0; i < colData.length; i++) {
        colData[i] = ((Vector) data.get(i)).get(0);
    }/*  w  w w .j ava2  s  .  c  o m*/
    Arrays.sort(colData, new ColumnSorter());
    for (int i = 0; i < colData.length; i++) {
        ((Vector) data.get(i)).set(0, colData[i]);
    }
    model.fireTableStructureChanged();
}

From source file:CompTest.java

public static void main(String args[]) {
    ArrayList u2 = new ArrayList();
    u2.add("Beautiful Day");
    u2.add("Stuck In A Moment You Can't Get Out Of");
    u2.add("Elevation");
    u2.add("Walk On");
    u2.add("Kite");
    u2.add("In A Little While");
    u2.add("Wild Honey");
    u2.add("Peace On Earth");
    u2.add("When I Look At The World");
    u2.add("New York");
    u2.add("Grace");

    Comparator comp = Comparators.stringComparator();
    Collections.sort(u2, comp);/*from ww w.  ja  v  a 2 s. c om*/
    System.out.println(u2);

    Arrays.sort(args, comp);
    System.out.print("[");
    for (int i = 0, n = args.length; i < n; i++) {
        if (i != 0)
            System.out.print(", ");
        System.out.print(args[i]);
    }
    System.out.println("]");
}

From source file:Main.java

public static void main(String args[]) {
    JTextComponent component = new JTextArea();

    // Process action list
    Action actions[] = component.getActions();
    // Define comparator to sort actions
    Comparator<Action> comparator = new Comparator<Action>() {
        public int compare(Action a1, Action a2) {
            String firstName = (String) a1.getValue(Action.NAME);
            String secondName = (String) a2.getValue(Action.NAME);
            return firstName.compareTo(secondName);
        }/*from  w  ww . j ava  2 s.co  m*/
    };
    Arrays.sort(actions, comparator);

    int count = actions.length;
    System.out.println("Count: " + count);
    for (int i = 0; i < count; i++) {
        System.out.printf("%28s : %s\n", actions[i].getValue(Action.NAME), actions[i].getClass().getName());
    }
}

From source file:Main.java

public static void main(String args[]) {
    JTextComponent component = new JTextField();

    // Process action list
    Action actions[] = component.getActions();
    // Define comparator to sort actions
    Comparator<Action> comparator = new Comparator<Action>() {
        public int compare(Action a1, Action a2) {
            String firstName = (String) a1.getValue(Action.NAME);
            String secondName = (String) a2.getValue(Action.NAME);
            return firstName.compareTo(secondName);
        }/*ww w .j  a  v a 2  s. c  om*/
    };
    Arrays.sort(actions, comparator);

    int count = actions.length;
    System.out.println("Count: " + count);
    for (int i = 0; i < count; i++) {
        System.out.printf("%28s : %s\n", actions[i].getValue(Action.NAME), actions[i].getClass().getName());
    }
}

From source file:ListActionsJTextPane.java

public static void main(String args[]) {
    JTextComponent component = new JTextPane();

    // Process action list
    Action actions[] = component.getActions();
    // Define comparator to sort actions
    Comparator<Action> comparator = new Comparator<Action>() {
        public int compare(Action a1, Action a2) {
            String firstName = (String) a1.getValue(Action.NAME);
            String secondName = (String) a2.getValue(Action.NAME);
            return firstName.compareTo(secondName);
        }/*from   w  w  w.  j a  v  a 2  s  .  c  om*/
    };
    Arrays.sort(actions, comparator);

    int count = actions.length;
    System.out.println("Count: " + count);
    for (int i = 0; i < count; i++) {
        System.out.printf("%28s : %s\n", actions[i].getValue(Action.NAME), actions[i].getClass().getName());
    }
}

From source file:ListActionsJEditorPane.java

public static void main(String args[]) {
    JTextComponent component = new JEditorPane();

    // Process action list
    Action actions[] = component.getActions();
    // Define comparator to sort actions
    Comparator<Action> comparator = new Comparator<Action>() {
        public int compare(Action a1, Action a2) {
            String firstName = (String) a1.getValue(Action.NAME);
            String secondName = (String) a2.getValue(Action.NAME);
            return firstName.compareTo(secondName);
        }//  w ww. j  a  va 2s  . c  o m
    };
    Arrays.sort(actions, comparator);

    int count = actions.length;
    System.out.println("Count: " + count);
    for (int i = 0; i < count; i++) {
        System.out.printf("%28s : %s\n", actions[i].getValue(Action.NAME), actions[i].getClass().getName());
    }
}