List of usage examples for java.util Collections sort
@SuppressWarnings({ "unchecked", "rawtypes" }) public static <T> void sort(List<T> list, Comparator<? super T> c)
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); System.out.println(u2);// w w w .j av a 2 s . c o m 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) { Map<String, Student> map = new HashMap<>(); map.put("s1", new Student(5, "A")); map.put("s2", new Student(4, "B")); map.put("s3", new Student(10, "C")); map.put("s4", new Student(2, "D")); Collection<Student> students = map.values(); List<Student> list = new ArrayList<>(students); Collections.sort(list, new MyComparator()); for (Iterator<Student> it = list.iterator(); it.hasNext();) { Student stdn = (Student) it.next(); System.out.println("Student id : " + stdn.id); System.out.println("Student Name : " + stdn.name); }/*from w ww . j a v a 2 s .c o m*/ }
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 ww w .j a v a2 s. 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:MyComparator.java
public static void main(String[] args) { TreeMap tm = new TreeMap(); tm.put(1, new Double(344.34)); tm.put(0, new Double(123.22)); tm.put(4, new Double(138.00)); tm.put(2, new Double(919.22)); tm.put(3, new Double(-119.08)); List<Map.Entry> valueList = new ArrayList(tm.entrySet()); Collections.sort(valueList, new MyComparator()); Iterator<Map.Entry> iterator = valueList.iterator(); while (iterator.hasNext()) { Map.Entry entry = iterator.next(); System.out.println("Value: " + entry.getValue()); }/*from w ww . ja v a 2s. com*/ }
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 ava 2s. co m*/ 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);//from w ww .j av a2 s. c om } 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:SortByMark.java
public static void main(String args[]) { List<Student> students = new ArrayList<Student>(); SortByMark sortByMark = new SortByMark(); students.add(new Student("A", new Double(34.34))); students.add(new Student("C", new Double(123.22))); students.add(new Student("B", new Double(13.00))); students.add(new Student("Z", new Double(99.22))); students.add(new Student("X", new Double(-19.08))); Collections.sort(students, sortByMark); for (Student student : students) { System.out.println(student); }/*from w w w . j av a2 s . c o m*/ }
From source file:SortByHouseNo.java
public static void main(String[] args) { String houseList[] = { "9-11", "9-01", "10-02", "10-01", "2-09", "3-88", "9-03", "9-3" }; HouseNo house = null;/*w w w . ja va 2 s . com*/ ArrayList<HouseNo> sortedList = new ArrayList<>(); for (String string : houseList) { String h = string.substring(0, string.indexOf('-')); String b = string.substring(string.indexOf('-') + 1); house = new HouseNo(h, b); sortedList.add(house); } System.out.println("Before Sorting :: "); for (HouseNo houseNo : sortedList) { System.out.println(houseNo); } Collections.sort(sortedList, new SortByHouseNo()); System.out.println("\n\nAfter Sorting HouseNo :: "); for (HouseNo houseNo : sortedList) { System.out.println(houseNo); } }
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 www. jav a2 s. c om 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()); } }
From source file:Person.java
public static void main(String[] args) { SimpleDateFormat df = new SimpleDateFormat("mm-dd-yyyy"); ArrayList<Person> people; people = new ArrayList<Person>(); try {/*from w ww. j a v a 2 s. c om*/ people.add(new Person("A", 9, df.parse("12-12-2014"))); people.add(new Person("B", 2, df.parse("1-12-2013"))); people.add(new Person("C", 4, df.parse("12-2-2012"))); } catch (ParseException e) { e.printStackTrace(); } Collections.sort(people, new CompId()); System.out.println("BY ID"); for (Person p : people) { System.out.println(p.toString()); } Collections.sort(people, new CompDate(false)); System.out.println("BY Date asc"); for (Person p : people) { System.out.println(p.toString()); } Collections.sort(people, new CompDate(true)); System.out.println("BY Date desc"); for (Person p : people) { System.out.println(p.toString()); } }