List of usage examples for java.util Comparator Comparator
Comparator
From source file:Main.java
public static void main(String[] args) { String[] strings = { "Here", "are", "some", "sample", "strings", "to", "be", "sorted" }; Arrays.sort(strings, new Comparator<String>() { public int compare(String s1, String s2) { int c = s2.length() - s1.length(); if (c == 0) c = s1.compareToIgnoreCase(s2); return c; }/*from www. j a va 2 s .c o m*/ }); for (String s : strings) System.out.print(s + " "); }
From source file:AnimationTester.java
public static void main(String[] args) { Integer[] values = new Integer[] { 1, 2, 7, 3, 5 }; Comparator<Integer> comp = new Comparator<Integer>() { public int compare(Integer d1, Integer d2) { return d1.compareTo(d2); }/*from w w w. ja v a2 s . c o m*/ }; MergeSorter.sort(values, comp); for (int i = 0; i < values.length; i++) { System.out.print(values[i] + " "); } }
From source file:Main.java
public static void main(String[] args) { // setup/* w w w. j a va 2s.c om*/ List<Integer> list = new ArrayList<Integer>(); list.add(4); list.add(3); // classic way list.sort(new Comparator<Integer>() { @Override public int compare(Integer a, Integer b) { return a - b; } }); System.out.println(list); // lambda way, the shortest way list.sort((a, b) -> a - b); System.out.println(list); }
From source file:Main.java
public static void main(String[] args) { // setup/*w ww . j a v a 2 s .c o m*/ List<Integer> list = new ArrayList<Integer>(); list.add(4); list.add(3); // classic way list.sort(new Comparator<Integer>() { @Override public int compare(Integer a, Integer b) { return a - b; } }); System.out.println(list); // lambda way Comparator<Integer> lambdaComparator = (Integer a, Integer b) -> b - a; list.sort(lambdaComparator); System.out.println(list); }
From source file:Main.java
public static void main(String[] args) { List<String> list = new ArrayList<>(); list.add("acowa"); list.add("cow"); list.add("aow"); Collections.sort(list, new Comparator<String>() { @Override//from w w w.ja v a 2 s. c o m public int compare(String o1, String o2) { if (o1.length() > o2.length()) { return 1; } else { return o1.compareTo(o2); } } }); System.out.println(list); }
From source file:MyObject.java
public static void main(String[] args) { Set<MyObject> set = new TreeSet<>(new Comparator<MyObject>() { @Override//from ww w .j a v a 2 s.c om public int compare(MyObject left, MyObject right) { return left.value - right.value; } }); set.add(new MyObject(1)); set.add(new MyObject(2)); set.add(new MyObject(3)); set.add(new MyObject(1)); // '1' is already in set System.out.println("size:" + set.size());// print 3 System.out.println(set.remove(new MyObject(3))); System.out.println("size:" + set.size()); }
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 w w . jav a 2s. 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); }/*from w ww . j ava2 s .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()); } }
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); }// w w w . j av 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); }//from www .j ava 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()); } }