comparator

How to test Comparator for JUnit test?

佐手、 提交于 2021-02-05 09:28:28
问题 I need to test the compare() method and i am confused on how. Can I see how to do this? public class MemberComparator implements Comparator<Member> { private final String clientId; public MemberComparator(String clientId) { this.clientId = clientId; } @Override public int compare(Member m1, Member m2) { if (m1.getClientId().startsWith(clientId)) { return m2.getClientId().startsWith(clientId) ? m1.getClientId().compareTo(m2.getClientId()) : -1; } else { return m2.getClientId().startsWith

How to test Comparator for JUnit test?

隐身守侯 提交于 2021-02-05 09:27:17
问题 I need to test the compare() method and i am confused on how. Can I see how to do this? public class MemberComparator implements Comparator<Member> { private final String clientId; public MemberComparator(String clientId) { this.clientId = clientId; } @Override public int compare(Member m1, Member m2) { if (m1.getClientId().startsWith(clientId)) { return m2.getClientId().startsWith(clientId) ? m1.getClientId().compareTo(m2.getClientId()) : -1; } else { return m2.getClientId().startsWith

Comparator using a String field of my class for comparison?

别等时光非礼了梦想. 提交于 2021-01-28 12:00:55
问题 I have a list of objects of type A, and I have to order it for a field of A, which is of type String. public class A{ public String field1; public Integer field2; ... } If I had to order for the int field would have done so: Collections.sort(listOfA, new Comparator<A>() { public int compare(A p1, A p2) { return p1.field2 - p2.field2); } }); But unfortunately need to order by the field of type String. How can I do this? 回答1: public int compare(A p1, A p2) { return p1.field2.compareTo( p2

How to sort by a field of class with its own comparator?

喜欢而已 提交于 2021-01-21 10:31:55
问题 I have a sample class, say Country : class Country { public String name; public int population; public Flag flag; ... } I have this Flag class defined somewhere else class Flag { String id; int pixel; ... } Now I create a separate comparator DefaultFlagRankingComparator() that can sort Flag by id. How can I sort a list of Country by Flag id, using this DefaultFlagRankingComparator()? 回答1: You can invoke the compare method of the Comparator with the flag field of each country.

How to sort by a field of class with its own comparator?

三世轮回 提交于 2021-01-21 10:31:35
问题 I have a sample class, say Country : class Country { public String name; public int population; public Flag flag; ... } I have this Flag class defined somewhere else class Flag { String id; int pixel; ... } Now I create a separate comparator DefaultFlagRankingComparator() that can sort Flag by id. How can I sort a list of Country by Flag id, using this DefaultFlagRankingComparator()? 回答1: You can invoke the compare method of the Comparator with the flag field of each country.

How to sort by a field of class with its own comparator?

别说谁变了你拦得住时间么 提交于 2021-01-21 10:31:02
问题 I have a sample class, say Country : class Country { public String name; public int population; public Flag flag; ... } I have this Flag class defined somewhere else class Flag { String id; int pixel; ... } Now I create a separate comparator DefaultFlagRankingComparator() that can sort Flag by id. How can I sort a list of Country by Flag id, using this DefaultFlagRankingComparator()? 回答1: You can invoke the compare method of the Comparator with the flag field of each country.

How to sort by a field of class with its own comparator?

人盡茶涼 提交于 2021-01-21 10:29:10
问题 I have a sample class, say Country : class Country { public String name; public int population; public Flag flag; ... } I have this Flag class defined somewhere else class Flag { String id; int pixel; ... } Now I create a separate comparator DefaultFlagRankingComparator() that can sort Flag by id. How can I sort a list of Country by Flag id, using this DefaultFlagRankingComparator()? 回答1: You can invoke the compare method of the Comparator with the flag field of each country.

Sort List<Map<String,Object>> based on value

蹲街弑〆低调 提交于 2021-01-19 09:03:43
问题 Basically I have a List<Map<String,Object>> , and I want to sort it by the values of certain key in the map. The problem is that I do not know the type... This map can contain Strings, Integers, Doubles, Floats etc.... I would like to sort it: So far List<Map<String,Object>> data = getResults(); Collections.sort(data, (o1, o2) -> (String.valueOf(o2.get("Field1"))) .compareTo((String.valueOf(o1.get("Field1"))))); It is not a great Idea since numbers are not properly sorted.... How can I handle

Sort List<Map<String,Object>> based on value

拟墨画扇 提交于 2021-01-19 09:01:26
问题 Basically I have a List<Map<String,Object>> , and I want to sort it by the values of certain key in the map. The problem is that I do not know the type... This map can contain Strings, Integers, Doubles, Floats etc.... I would like to sort it: So far List<Map<String,Object>> data = getResults(); Collections.sort(data, (o1, o2) -> (String.valueOf(o2.get("Field1"))) .compareTo((String.valueOf(o1.get("Field1"))))); It is not a great Idea since numbers are not properly sorted.... How can I handle

Why is the same character compared twice by changing its case to UPPER and then to lower?

爷,独闯天下 提交于 2021-01-19 03:20:26
问题 The below code is in Class String in java. I don't understand why the characters from two different strings are compared twice . at first by doing upper case and if that fails by doing lower case. My Question here is, is it required? If yes, why? public static final Comparator<String> CASE_INSENSITIVE_ORDER = new CaseInsensitiveComparator(); private static class CaseInsensitiveComparator implements Comparator<String>, java.io.Serializable { // use serialVersionUID from JDK 1.2.2 for