comparator

How to sort ArrayList using Comparator? [duplicate]

早过忘川 提交于 2019-12-17 12:04:31
问题 This question already has answers here : Sort ArrayList of custom Objects by property (25 answers) Closed 5 years ago . I have a Class Student that Implements a static method public static Comparator<Student> getCompByName() that returns a new comparator object for Student that compares 2 Students objects by the attribute 'name'. I need to now test this by sorting a students ArrayList by 'name' using my function getCompByName(). Here is my Comparator method in my Student class. public static

How to sort ArrayList using Comparator? [duplicate]

不问归期 提交于 2019-12-17 12:04:09
问题 This question already has answers here : Sort ArrayList of custom Objects by property (25 answers) Closed 5 years ago . I have a Class Student that Implements a static method public static Comparator<Student> getCompByName() that returns a new comparator object for Student that compares 2 Students objects by the attribute 'name'. I need to now test this by sorting a students ArrayList by 'name' using my function getCompByName(). Here is my Comparator method in my Student class. public static

Comparable and Comparator contract with regards to null

你离开我真会死。 提交于 2019-12-17 06:49:21
问题 Comparable contract specifies that e.compareTo(null) must throw NullPointerException . From the API: Note that null is not an instance of any class, and e.compareTo(null) should throw a NullPointerException even though e.equals(null) returns false . On the other hand, Comparator API mentions nothing about what needs to happen when comparing null . Consider the following attempt of a generic method that takes a Comparable , and return a Comparator for it that puts null as the minimum element.

Using Comparable for multiple dynamic fields of VO in java

一个人想着一个人 提交于 2019-12-17 06:45:10
问题 I have a class public class StudentVO { int age; String name; } I used the same class in two different areas. At one place i need to sort based on the age. In another place I need to sort based on the name and in another place i may need sorting based on both age and name. How can I do this? If one field I can override compareTo() . Is it possible to do this? 回答1: 1)You should write two Comparator for sorting on age and name separately, and then use the Collections.sort(List,Comparator).

Simple way to sort strings in the (case sensitive) alphabetical order

扶醉桌前 提交于 2019-12-17 06:14:08
问题 I need to sort list of strings in the alphabetical order: List<String> list = new ArrayList(); list.add("development"); list.add("Development"); list.add("aa"); list.add("AA"); list.add("Aa"); A common way to do it is to use comparator: Collections.sort(list, String.CASE_INSENSITIVE_ORDER); The problem of the CaseInsensitiveComparator that “AA” is equals to “aa”. Strings appear in the result according to the order of adding for the same values, and it is not correct: "aa","AA","Aa",

Java Comparator class to sort arrays

£可爱£侵袭症+ 提交于 2019-12-17 02:31:41
问题 Say, we have the following 2-dimensional array: int camels[][] = new int[n][2]; How should Java Comparator class be declared to sort the arrays by their first elements in decreasing order using Arrays.sort(camels, comparator) ? The compare function for reference is: @Override public int compare(int[] a, int [] b) { return b[0] - a[0]; } 回答1: [...] How should Java Comparator class be declared to sort the arrays by their first elements in decreasing order [...] Here's a complete example using

Java : Comparable vs Comparator [duplicate]

China☆狼群 提交于 2019-12-16 20:15:47
问题 This question already has answers here : Closed 9 years ago . Possible Duplicates: difference between compare() and compareTo() Java: What is the difference between implementing Comparable and Comparator? What are the keys differences between Comparable and Comparator. and which is preferred over the other in what scenarios? Thanks Updated - GOOD LINK WITH EXAMPLE!! http://www.digizol.com/2008/07/java-sorting-comparator-vs-comparable.html 回答1: When your class implements Comparable, the

How get List from Set and Comparator

*爱你&永不变心* 提交于 2019-12-14 03:43:29
问题 What is the "good" (and why ?) solution to get a List from a Set and sorted against a given Comparator ? 回答1: Set<Object> set = new HashSet<Object>(); // add stuff List<Object> list = new ArrayList<Object>(set); Collections.sort(list, new MyComparator()); 回答2: Just construct it. The ArrayList has a constructor taking another Collection. Set<Foo> set = new TreeSet<Foo>(new FooComparator<Foo>()); // Fill it. List<Foo> list = new ArrayList<Foo>(set); // Here's your list with items in the same

How to sort a list of objects according to two parameters to compare at Java?

流过昼夜 提交于 2019-12-13 19:07:48
问题 I have a class like that: public class Zern extends Something{ private int costA; private int costB; public int getcostA() { return costA; } public void setcostA(int costA) { this.costA = costA; } public int getcostB() { return costB; } public void setcostB(int costB) { this.costB = costB; } } I have a list that holds that kind of objects: private List<Zern> zerns = new ArrayList<Zern>(MAX_ZERN_SIZE); I will add new objects to my list however I always want to have a ordered list according to

JTable AutoCreateRowSorter sorting Numbers As Strings

ⅰ亾dé卋堺 提交于 2019-12-13 17:01:56
问题 I have a JTable: JTable table = new JTable(); String[] colNames = {"c1"}; DefaultTableModel model = new DefaultTableModel(); Integer[] x = new Integer[10]; int[] xi = {0,1,2,3,4,5,6,7,8,9}; for (int i=0; i<10; i++){ x[i]=new Integer(xi[i]); }model.addColumn("c1"); table.setModel(model); table.setEnabled(false); table.setAutoCreateRowSorter(true); JScrollPane scrollpane = new JScrollPane(table); contentPane.add(scrollpane); Now when I load this and click on a column title the rows sort as if