comparator

Sorting Object with Comparator gives Null Pointer

旧街凉风 提交于 2020-01-14 12:46:26
问题 I am trying sort the ArrayList with 3 Card in it. I am doing this with a Comparator. (Is this overkill)? Card.getRank() returns an integer between 2 and 14. I have absolutely no idea where I am going wrong. I have done this successfully before, and compared with my other code, and it seems the same. I would greatly appreciate if someone could spread some light on this! public int getHand(Card c1, Card c2, Card c3) { ArrayList<Card> hand = new ArrayList<Card>(); hand.add(c1); hand.add(c2);

Java Multi-Level Comparator

喜夏-厌秋 提交于 2020-01-11 12:34:47
问题 I am working on a problem where I must take these "Song-artist pairs" from an input file and sort alphabetically. The guidelines to the sorting goes like so: Should sort the song-artist pairs by the name of the author first. Once sorted by the artists, if there are multiple songs by the same artist, they should be sort alphabetically as well. If the artists name starts with "The", ignore it for sorting purposes. My problem is that when I am sorting these, I am able to get the artists sorted

Why is my compareTo crashing with a general contract violation error? [duplicate]

本小妞迷上赌 提交于 2020-01-04 06:09:00
问题 This question already has answers here : Java error: Comparison method violates its general contract (9 answers) Closed 3 years ago . I'm trying to sort my custom NewsAdapter by a Date property of the Articles_Map object, and I've noticed that in cases with bigger data sets my app crashes with a java.lang.IllegalArgumentException: Comparison method violates its general contract! error. I'm not sure if that error happens because of an int overflow, or if it is indeed related to the transitive

Move specific items to the end of a list

对着背影说爱祢 提交于 2020-01-02 03:51:32
问题 I have an ArrayList in Java : {"deleteItem", "createitem", "exportitem", "deleteItems", "createItems"} I want to move all string which contains delete to the end of the list, so I would get the next: {"createitem", "exportitem", "createItems", "deleteItem", "deleteItems"}` I can create two sublists - one for the words which contain the 'delete' word, and one for the others, and then merge them, but I search for a more efficient way. 回答1: Use custom Comparator: List<String> strings = Arrays

In Java, sort hash map by its key.length()

主宰稳场 提交于 2020-01-02 02:55:06
问题 i have a hashmap like this: HashMap<String,Integer> map = new HashMap<String,Integer>(); map.put("java",4); map.put("go",2); map.put("objective-c",11); map.put("c#",2); now i want to sort this map by its key length, if two keys length are equal (e.g go and c# both length 2), then sorted by alphba order. so the outcome i expect to get is something like: printed result: objective-c, 11 java, 4 c#, 2 go, 2 here is my own attamp, but it doesnt work at all... HashMap<String,Integer> map = new

Implement Comparator for primitive boolean type?

笑着哭i 提交于 2020-01-02 00:32:09
问题 I need some classes implements Comparator , and for one I want to compare primitive boolean (not Boolean ) values. IF it was a B oolean, I would just return boolA.compareTo(boolB); which would return 0, -1 or 1. But how can I do this with primitives? 回答1: You can look up how it is implemented for the java.lang.Boolean, since that class, naturally, uses a primitive boolean as well: public int compareTo(Boolean b) { return (b.value == value ? 0 : (value ? 1 : -1)); } 回答2: Since Java 7, the

std::is_sorted and strictly less comparison?

房东的猫 提交于 2020-01-01 08:33:38
问题 I do not understand well the std::is_sorted algorithm and its default behaviour. If we look to cppreference, it says that by default std::is_sorted uses the < operator. Instead of that, I find that using <= would be natural. But my problem is that for the following list of numbers : 1 2 3 3 4 5 it will return true , even if 3 < 3 should be false . How is that possible ? EDIT: its seems to be worse than what I thought, because passing std::less_equal<int> will return false in that case... What

std::is_sorted and strictly less comparison?

别等时光非礼了梦想. 提交于 2020-01-01 08:33:18
问题 I do not understand well the std::is_sorted algorithm and its default behaviour. If we look to cppreference, it says that by default std::is_sorted uses the < operator. Instead of that, I find that using <= would be natural. But my problem is that for the following list of numbers : 1 2 3 3 4 5 it will return true , even if 3 < 3 should be false . How is that possible ? EDIT: its seems to be worse than what I thought, because passing std::less_equal<int> will return false in that case... What

Java FX table column sorting with Comparator does not work

有些话、适合烂在心里 提交于 2020-01-01 07:07:25
问题 In Java FX I would like to display this Model in a sorted TableView: public class ProfilZuordnungTableRowModel { private int id; private double kundenwert; private String kundenwertFormatted; private BooleanProperty selected; } I would like to integrate a table column sorting with the column "Kundenwert". The displayed value should be the attribute "kundenwertFormatted" (String) and for sorting the attribute "kundenwert" (Double) should be used. So I wrote a comparator: class

How write universal comparator which can make sorting through all necessary fields?

做~自己de王妃 提交于 2020-01-01 00:35:06
问题 I needed to implement sorting on all fields of my class I wrote a comparator for each field in my class. But I had to write a individual comparator for each field. I think that it is not very correctly. How write for my class a single universal comparator which can make sorting through all fields? My entity: public class User { private long id; private String name; private int age; ..................... My comparators: public class UserComparatorById implements Comparator<User> { public int