comparator

Java Multiple Array Sorting

你。 提交于 2019-12-13 16:15:17
问题 Hi I currently have 4 arrays all holding different data the issue i have run into is that i want to sort one of the arrays alphabetically which normally i would just do like this class IgnoreCaseComparator implements Comparator<String> { public int compare(String strA, String strB) { return strA.compareToIgnoreCase(strB); } } IgnoreCaseComparator icc = new IgnoreCaseComparator(); java.util.Collections.sort(ARRAY,icc); Where ARRAY would be the array I want to sort Alphabetically however I need

How to perform a series of sort operation on arraylist (multiple sort criteria)

China☆狼群 提交于 2019-12-13 14:26:15
问题 I have an arrayList of objects and I want to run a series of sort operations on this list. I would want to sort them by first by name, and if two names are same than sort them by id, for example. How can i implement it? This is my code Comparator<Ticket> mc; mc = new TicketIdComparator(); Collections.sort(tickets, mc); final class TicketIdComparator implements Comparator<Ticket> { @Override public int compare(Ticket ticket1, Ticket ticket2) { String TicketId1 = ((Ticket) ticket1).getNumber();

Using comparator for several characteristics in java

你说的曾经没有我的故事 提交于 2019-12-13 10:17:59
问题 I have a list to be sorted but it cannot be done if values are represented as strings. Example: to sort: OB123, OB1212, Maintenance, Daily check, OB123 desired result: Daily check, Maintenance, OB123, OB123, OB1212 if values are strings result is: Daily check, Maintenance, OB1212, OB123,OB123 Therefore I need to use comparator to first sort aircraft numbers such OB123 by their carrier(OB), than by their number (123) and sometimes suffix (""). And after that I would like to compare the whole

comparator for LinkedHashMap

我是研究僧i 提交于 2019-12-13 09:22:46
问题 i am making a program to read data from excel files and store them in tables. But since I am new to Comparators in Java i have difficulties in making one of them. Here is my issue, I have managed to read all the data from excel files as a string and store them in a table. But my project is to store them in table by ascending IDs. Can someone help me to create a Comparator to compare LinkedHashmaps and store them by their IDs? The data that I have to store is like the below: ID Name Salary 50

Java Comparator always reading values as Strings

荒凉一梦 提交于 2019-12-13 06:48:35
问题 In trying to figure out the answer to another question I asked (How to sort a jtable with null values always at the end), I ran into another problem. I am implementing a custom TableRowSorter that creates a custom Comparator . However, the Comparator always seems to read every Object as a type String . This one has me baffled. If you'll notice in the SSCCE below, the line System.out.println(o1.getClass() + " - " + o2.getClass()); always yeilds the output class java.lang.String - class java

Class to store multiple comparators

断了今生、忘了曾经 提交于 2019-12-13 04:46:53
问题 I have a task to sort files (list of files), dependng on user input by name, size, date modified, size, type, and other types of conditions can be added in future. So if user inputs "dir /sort=esdn" (extension, size, date, name) That means sort by extension, where program can't decide sort by size... I thougth it would be messy to have all those comparators in same class or as lambda expressions, so i had idea of creating a new clas where all comparisons code would be public class Comparisons

Is this a correct way of sorting by title, position and then order by using a Comparator?

六月ゝ 毕业季﹏ 提交于 2019-12-13 02:56:57
问题 Consider this class. public class DynamicField implements Comparable<DynamicField> { String title; int position; int order; @Override public int compareTo(DynamicField o) { if(position < o.position) return -1; if(position > o.position) return 1; if(order < o.order) return -1; if(order > o.order) return 1; return title.compareTo(o.title); } } Is the compareTo method correct if I want to sort by title, position and then order? 回答1: No, you're making comparisons in an incorrect order.

Instantiating a TreeMap using a Comparator which should be able to access the said TreeMap

☆樱花仙子☆ 提交于 2019-12-13 02:26:56
问题 I'm trying to instantiate a TreeMap using a Comparator which should be able to access the said TreeMap , i.e. the one it will be used for (I guess that " will " must precisly be the problem...): final Map<String, Integer> map = new TreeMap<String, Integer>(new Comparator<String>() { @Override public int compare(String o1, String o2) { Integer i = map.get(o1); // Error: "the local variable map may not have been initialized" return ...; } }); I can get why this error occurs, since when

Sort date strings in map [duplicate]

醉酒当歌 提交于 2019-12-13 00:55:27
问题 This question already has answers here : Sorting a map by date key in Java (2 answers) Closed 3 years ago . Hi I want to sort map objects based on dates below is the map testMap.put("06/15/2015", 1); testMap.put("05/15/2015", 2); testMap.put("01/15/2016", 4); testMap.put("07/15/2015", 3); testMap.put("02/15/2016", 5); I need output like below sequence: 05/15/2015 06/15/2015 07/15/2015 01/15/2016 02/15/2016 I need ouput like sorted months in 2015 and then starting 2016 months in sorted order I

Sorting an ArrayList<String> with custom Comparator

喜夏-厌秋 提交于 2019-12-13 00:31:39
问题 I am trying to sort an ArrayList<String> using custom Comparator . My requirement is that XX String should be the first String, others should follow natural ordering. I need : [XX, XX, 1, 5, 9, A, D, G, Q, Z] What I am getting is [1, 5, 9, A, D, G, Q, Z, XX, XX] Following is my code: public class Test { public static void main(String[] args) { List<String> list = new ArrayList<String>(); list.add("Z"); list.add("5"); list.add("D"); list.add("G"); list.add("XX"); list.add("9"); list.add("Q");