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

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

    // 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  av  a  2 s. com
    };
    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:ComparatorExampleForBuildInDataType.java

public static void main(String args[]) {
    ComparatorExampleForBuildInDataType example = new ComparatorExampleForBuildInDataType();
    example.createComparators();//  ww w  . ja v a  2 s.c o m

    Arrays.sort(boolParams, boolComp);

    example.printArray(boolParams);

    Arrays.sort(stringParams);

    example.printArray(stringParams);

    Arrays.sort(stringParams, fixedComp);

    example.printArray(stringParams);
}

From source file:DirList3.java

public static void main(final String[] args) {
    File path = new File(".");
    String[] list;//from  w ww . j a va  2s.  c  o 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, new AlphabeticComparator());
    for (int i = 0; i < list.length; i++)
        System.out.println(list[i]);
}

From source file:DirList2.java

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

From source file:Person.java

public static void main(String[] args) {
    Person[] authors = { new Person("A", "S"), new Person("J", "G"), new Person("T", "C"), new Person("C", "S"),
            new Person("P", "C"), new Person("B", "B") };
    System.out.println("Original order:");
    for (Person author : authors) {
        System.out.println(author);
    }/* w w w.  j  a v a 2s .  c  o  m*/
    Arrays.sort(authors, new ComparePersons());
    System.out.println("\nOrder after sorting using comparator:");
    for (Person author : authors) {
        System.out.println(author);
    }
    Arrays.sort(authors); // Sort using Comparable method
    System.out.println("\nOrder after sorting using Comparable method:");
    for (Person author : authors) {
        System.out.println(author);
    }
}

From source file:com.github.rinde.gpem17.Train.java

public static void main(String[] args) {
    if (args.length == 0) {
        run("files/config/gpem17.params");
    } else {/*from  w w  w  . j  a  v a2s.c  o  m*/
        for (String file : args) {
            File f = new File(file);
            if (f.isDirectory()) {
                File[] paramFiles = f.listFiles(new FilenameFilter() {
                    @Override
                    public boolean accept(File dir, String name) {
                        return name.endsWith(".params");
                    }
                });

                Arrays.sort(paramFiles, new Comparator<File>() {
                    @Override
                    public int compare(File o1, File o2) {
                        return o1.getName().compareTo(o2.getName());
                    }
                });

                for (File paramFile : paramFiles) {
                    run(paramFile.getPath());
                }
            } else {
                run(file);
            }
        }
    }
}

From source file:Person.java

public static void main(String[] args) {
    Person[] authors = { new Person("A", "S"), new Person("J", "G"), new Person("T", "C"), new Person("C", "S"),
            new Person("P", "C"), new Person("B", "B") };
    System.out.println("Original order:");
    for (Person author : authors) {
        System.out.println(author);
    }//  w  w  w .j  a v  a 2  s .  c o m
    Arrays.sort(authors, new ComparePersons()); // Sort using comparator
    System.out.println("\nOrder after sorting using comparator:");
    for (Person author : authors) {
        System.out.println(author);
    }
    Arrays.sort(authors); // Sort using Comparable method
    System.out.println("\nOrder after sorting using Comparable method:");
    for (Person author : authors) {
        System.out.println(author);
    }
}

From source file:org.eclipse.swt.snippets.Snippet192.java

public static void main(String[] args) {
    // initialize data with keys and random values
    int size = 100;
    Random random = new Random();
    final int[][] data = new int[size][];
    for (int i = 0; i < data.length; i++) {
        data[i] = new int[] { i, random.nextInt() };
    }//from w  ww . j a v  a 2  s.c  o  m
    // create a virtual table to display data
    Display display = new Display();
    Shell shell = new Shell(display);
    shell.setText("Snippet 192");
    shell.setLayout(new FillLayout());
    final Table table = new Table(shell, SWT.VIRTUAL);
    table.setHeaderVisible(true);
    table.setLinesVisible(true);
    table.setItemCount(size);
    final TableColumn column1 = new TableColumn(table, SWT.NONE);
    column1.setText("Key");
    column1.setWidth(200);
    final TableColumn column2 = new TableColumn(table, SWT.NONE);
    column2.setText("Value");
    column2.setWidth(200);
    table.addListener(SWT.SetData, e -> {
        TableItem item = (TableItem) e.item;
        int index = table.indexOf(item);
        int[] datum = data[index];
        item.setText(new String[] { Integer.toString(datum[0]), Integer.toString(datum[1]) });
    });
    // Add sort indicator and sort data when column selected
    Listener sortListener = e -> {
        // determine new sort column and direction
        TableColumn sortColumn = table.getSortColumn();
        TableColumn currentColumn = (TableColumn) e.widget;
        int dir = table.getSortDirection();
        if (sortColumn == currentColumn) {
            dir = dir == SWT.UP ? SWT.DOWN : SWT.UP;
        } else {
            table.setSortColumn(currentColumn);
            dir = SWT.UP;
        }
        // sort the data based on column and direction
        final int index = currentColumn == column1 ? 0 : 1;
        final int direction = dir;
        Arrays.sort(data, (a, b) -> {
            if (a[index] == b[index])
                return 0;
            if (direction == SWT.UP) {
                return a[index] < b[index] ? -1 : 1;
            }
            return a[index] < b[index] ? 1 : -1;
        });
        // update data displayed in table
        table.setSortDirection(dir);
        table.clearAll();
    };
    column1.addListener(SWT.Selection, sortListener);
    column2.addListener(SWT.Selection, sortListener);
    table.setSortColumn(column1);
    table.setSortDirection(SWT.UP);
    shell.setSize(shell.computeSize(SWT.DEFAULT, SWT.DEFAULT).x, 300);
    shell.open();
    while (!shell.isDisposed()) {
        if (!display.readAndDispatch())
            display.sleep();
    }
    display.dispose();
}

From source file:Person.java

public static void main(String[] args) {
    Person[] persons = new Person[4];
    persons[0] = new Person();
    persons[0].setFirstName("A");
    persons[0].setLastName("X");
    persons[0].setAge(56);/*from  ww  w.ja v  a  2  s.co  m*/

    persons[1] = new Person();
    persons[1].setFirstName("S");

    persons[1].setLastName("C");
    persons[1].setAge(8);

    persons[2] = new Person();
    persons[2].setFirstName("E");
    persons[2].setLastName("H");
    persons[2].setAge(16);

    persons[3] = new Person();
    persons[3].setFirstName("B");
    persons[3].setLastName("Q");
    persons[3].setAge(69);

    System.out.println("Natural Order");
    for (int i = 0; i < 4; i++) {
        Person person = persons[i];
        String lastName = person.getLastName();
        String firstName = person.getFirstName();
        int age = person.getAge();
        System.out.println(lastName + ", " + firstName + ". Age:" + age);
    }

    Arrays.sort(persons, new LastNameComparator());
    System.out.println();
    System.out.println("Sorted by last name");
    for (int i = 0; i < 4; i++) {
        Person person = persons[i];
        String lastName = person.getLastName();
        String firstName = person.getFirstName();
        int age = person.getAge();
        System.out.println(lastName + ", " + firstName + ". Age:" + age);
    }
    Arrays.sort(persons, new FirstNameComparator());
    System.out.println();
    System.out.println("Sorted by first name");
    for (int i = 0; i < 4; i++) {
        Person person = persons[i];
        String lastName = person.getLastName();
        String firstName = person.getFirstName();
        int age = person.getAge();
        System.out.println(lastName + ", " + firstName + ". Age:" + age);
    }

    Arrays.sort(persons);
    System.out.println();
    System.out.println("Sorted by age");
    for (int i = 0; i < 4; i++) {
        Person person = persons[i];
        String lastName = person.getLastName();
        String firstName = person.getFirstName();
        int age = person.getAge();
        System.out.println(lastName + ", " + firstName + ". Age:" + age);
    }
}

From source file:TableSortTwoDirection.java

public static void main(String[] args) {
    // initialize data with keys and random values
    int size = 100;
    Random random = new Random();
    final int[][] data = new int[size][];
    for (int i = 0; i < data.length; i++) {
        data[i] = new int[] { i, random.nextInt() };
    }//from   w  w  w . ja va  2  s.  c o  m
    // create a virtual table to display data
    Display display = new Display();
    Shell shell = new Shell(display);
    shell.setLayout(new FillLayout());
    final Table table = new Table(shell, SWT.VIRTUAL);
    table.setHeaderVisible(true);
    table.setLinesVisible(true);
    table.setItemCount(size);
    final TableColumn column1 = new TableColumn(table, SWT.NONE);
    column1.setText("Key");
    column1.setWidth(200);
    final TableColumn column2 = new TableColumn(table, SWT.NONE);
    column2.setText("Value");
    column2.setWidth(200);
    table.addListener(SWT.SetData, new Listener() {
        public void handleEvent(Event e) {
            TableItem item = (TableItem) e.item;
            int index = table.indexOf(item);
            int[] datum = data[index];
            item.setText(new String[] { Integer.toString(datum[0]), Integer.toString(datum[1]) });
        }
    });
    // Add sort indicator and sort data when column selected
    Listener sortListener = new Listener() {
        public void handleEvent(Event e) {
            // determine new sort column and direction
            TableColumn sortColumn = table.getSortColumn();
            TableColumn currentColumn = (TableColumn) e.widget;
            int dir = table.getSortDirection();
            if (sortColumn == currentColumn) {
                dir = dir == SWT.UP ? SWT.DOWN : SWT.UP;
            } else {
                table.setSortColumn(currentColumn);
                dir = SWT.UP;
            }
            // sort the data based on column and direction
            final int index = currentColumn == column1 ? 0 : 1;
            final int direction = dir;
            Arrays.sort(data, new Comparator() {
                public int compare(Object arg0, Object arg1) {
                    int[] a = (int[]) arg0;
                    int[] b = (int[]) arg1;
                    if (a[index] == b[index])
                        return 0;
                    if (direction == SWT.UP) {
                        return a[index] < b[index] ? -1 : 1;
                    }
                    return a[index] < b[index] ? 1 : -1;
                }
            });
            // update data displayed in table
            table.setSortDirection(dir);
            table.clearAll();
        }
    };
    column1.addListener(SWT.Selection, sortListener);
    column2.addListener(SWT.Selection, sortListener);
    table.setSortColumn(column1);
    table.setSortDirection(SWT.UP);
    shell.setSize(shell.computeSize(SWT.DEFAULT, SWT.DEFAULT).x, 300);
    shell.open();
    while (!shell.isDisposed()) {
        if (!display.readAndDispatch())
            display.sleep();
    }
    display.dispose();
}