Example usage for java.util Comparator Comparator

List of usage examples for java.util Comparator Comparator

Introduction

In this page you can find the example usage for java.util Comparator Comparator.

Prototype

Comparator

Source Link

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 ww . j a  v a 2  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:Main.java

public static void main(String[] args) {
    Comparator<String> ipComparator = new Comparator<String>() {
        @Override/*from  w w w  .  j  a  v  a  2s. c  o  m*/
        public int compare(String ip1, String ip2) {
            return toNumeric(ip1).compareTo(toNumeric(ip2));
        }
    };
    SortedSet<String> ips = new TreeSet<String>(ipComparator);
    ips.addAll(Arrays.asList("192.168.0.1", "192.168.0.2", "192.168.0.9", "9.9.9.9", "127.0.0.1"));
    System.out.println(ips);
}

From source file:Main.java

License:asdf

public static void main(String[] args) {
    Map<String, String> map = new HashMap<String, String>();

    map.put("KeyA", "erty");
    map.put("KeyC", "trwe");
    map.put("KeyD", "wert");
    map.put("KeyB", "fdsd");
    map.put("KeyS", "dsa");
    map.put("KeyE", "fdas");
    map.put("KeyG", "asdf");

    System.out.println(sort(map, new Comparator<String>() {
        @Override/*from   w  w w  .  ja v a  2 s. c om*/
        public int compare(String o1, String o2) {
            return o1.compareTo(o2);
        }
    }));
}

From source file:Main.java

public static void main(String args[]) {
    JFrame frame = new JFrame("Reverse Sample");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setLayout(new GridLayout(3, 3));
    for (int i = 0; i < 9; i++) {
        JButton button = new JButton(Integer.toString(i));
        frame.add(button);/*from  w w w . ja v a2  s  . com*/
    }
    final Container contentPane = frame.getContentPane();
    Comparator<Component> comp = new Comparator<Component>() {
        public int compare(Component c1, Component c2) {
            Component comps[] = contentPane.getComponents();
            List list = Arrays.asList(comps);
            int first = list.indexOf(c1);
            int second = list.indexOf(c2);
            return second - first;
        }
    };
    FocusTraversalPolicy policy = new SortingFocusTraversalPolicy(comp);
    frame.setFocusTraversalPolicy(policy);
    frame.setSize(300, 200);
    frame.setVisible(true);
}

From source file:MainClass.java

public static void main(String args[]) {
    JFrame frame = new JFrame("Reverse Sample");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    frame.setLayout(new GridLayout(3, 3));

    for (int i = 9; i > 0; i--) {
        JButton button = new JButton(Integer.toString(i));
        frame.add(button, 0);//  w w w  . java  2s . c  om
    }

    final Container contentPane = frame.getContentPane();
    Comparator<Component> comp = new Comparator<Component>() {
        public int compare(Component c1, Component c2) {
            Component comps[] = contentPane.getComponents();
            List list = Arrays.asList(comps);
            int first = list.indexOf(c1);
            int second = list.indexOf(c2);
            return second - first;
        }
    };

    FocusTraversalPolicy policy = new SortingFocusTraversalPolicy(comp);
    frame.setFocusTraversalPolicy(policy);

    frame.setSize(300, 200);
    frame.setVisible(true);
}

From source file:Main.java

public static void main(String[] args) {
    List<String> yearList = new ArrayList<>(25);
    yearList.add("042015");
    yearList.add("052015");
    yearList.add("062015");
    yearList.add("072015");
    yearList.add("082015");
    yearList.add("092015");
    yearList.add("102010");
    yearList.add("112010");
    yearList.add("122010");
    yearList.add("012015");
    yearList.add("022015");
    yearList.add("032015");

    Collections.sort(yearList, new Comparator<String>() {
        private DateFormat format = new SimpleDateFormat("MMyyyy");

        @Override/*from  w  w  w.j av  a 2s  .  c o m*/
        public int compare(String o1, String o2) {
            int result = 0;
            try {
                Date d1 = format.parse(o1);
                try {
                    Date d2 = format.parse(o2);
                    result = d1.compareTo(d2);
                } catch (ParseException ex) {
                    result = -1;
                }
            } catch (ParseException ex) {
                result = 1;
            }
            return result;
        }
    });
    System.out.println(yearList);
}

From source file:PriorityQueueTester.java

public static void main(String[] args) {

    PriorityQueue<Integer> pq = new PriorityQueue<Integer>(20, new Comparator<Integer>() {
        public int compare(Integer i, Integer j) {
            int result = i % 2 - j % 2;
            if (result == 0)
                result = i - j;//from  w  ww  .j av  a2  s.  c  o m
            return result;
        }
    });

    // Fill up with data, in an odd order
    for (int i = 0; i < 20; i++) {
        pq.offer(20 - i);
    }

    // Print out and check ordering
    for (int i = 0; i < 20; i++) {
        System.out.println(pq.poll());
    }
}

From source file:Main.java

public static void main(final String[] args) {
    final List<String> asu = new ArrayList<String>();
    asu.add("2");
    asu.add("11");
    asu.add("7");
    asu.add("10");
    asu.add("7");
    asu.add("12");
    asu.add("2");
    asu.add("11");
    asu.add("11");
    asu.add("7");
    asu.add("7");
    asu.add("7");

    List<String> list = new ArrayList<String>();
    Map<String, Integer> counts = new HashMap<String, Integer>();
    list.addAll(asu);/* w  w  w.  j a v a2s  .com*/
    for (String item : list) {
        Integer count = counts.get(item);
        if (count == null) {
            count = 1;
        } else {
            count = count + 1;
        }
        counts.put(item, count);
    }
    Collections.sort(asu, new Comparator<String>() {
        @Override
        public int compare(final String left, final String right) {
            int result = counts.get(left).compareTo(counts.get(right));
            if (result == 0) {
                result = left.compareTo(right);
            }
            return result;
        }
    });
    System.out.println(asu);
}

From source file:common.ReverseWordsCount.java

public static void main(String[] args) throws IOException {
    List<String> readLines = FileUtils.readLines(new File("G:\\\\LTNMT\\LTNMT\\sougou\\sougou2500.txt"));
    Map<String, Integer> words = new HashMap<>();

    for (String line : readLines) {
        String[] split = line.split(" ");
        for (String wprd : split) {
            Integer get = words.get(wprd);
            if (get == null) {
                words.put(wprd, 1);/*  w  ww .j  a  va2 s.  c o m*/
            } else {
                words.put(wprd, get + 1);
            }
        }
    }
    Set<Map.Entry<String, Integer>> entrySet = words.entrySet();
    List<Map.Entry<String, Integer>> reverseLists = new ArrayList<>(entrySet);
    Collections.sort(reverseLists, new Comparator<Map.Entry<String, Integer>>() {
        @Override
        public int compare(Map.Entry<String, Integer> o1, Map.Entry<String, Integer> o2) {
            return o2.getValue().compareTo(o1.getValue());
        }
    });
    PrintStream ps = new PrintStream("c:/reverseWords.txt");
    for (Map.Entry<String, Integer> teEntry : reverseLists) {
        ps.println(teEntry.getKey() + " " + teEntry.getValue());
    }
    ps.close();
}

From source file:Level.java

public static void main(String[] args) {
    Person one = new Person(Level.HIGH, "A");
    Person two = new Person(Level.MEDIUM, "B");
    Person three = new Person(Level.LOW, "C");
    Person four = new Person(Level.HIGH, "D");
    Person five = new Person(Level.MEDIUM, "E");
    Person six = new Person(Level.LOW, "F");
    Person seven = new Person(Level.LOW, "G");

    List<Person> persons = new ArrayList<Person>();
    persons.add(one);//from  w w  w .jav a 2 s  . co  m
    persons.add(two);
    persons.add(three);
    persons.add(four);
    persons.add(five);
    persons.add(six);
    persons.add(seven);

    Collections.sort(persons, new Comparator<Person>() {
        @Override
        public int compare(Person person1, Person person2) {
            if (person1.getSeverity() == person2.getSeverity()) {
                return person1.getName().compareTo(person2.getName());
            } else {
                return person1.getSeverity().compareTo(person2.getSeverity());
            }
        }
    });
    for (Person person : persons) {
        System.out.println(person.getName() + " " + person.getSeverity());
    }
}