Reputation: 17
There is a list of strings and each string has 4 set of values based on which they will be sorted.
1. Serial Number
2. State Example: It should come in the order("cooked","boiled","fresh","raw")
3. Date(Complete ISO-8601 DATE)
4. Alphabetically
I first want to sort a List by its serial number. Then if two or more values have the same serial number, I want to sort them by an already defined order of strings (STATE HERE). Now sort each duplicate category of the state by date and time. Now sort each duplicate date/time alphabetically.
So basically there are 4 levels of sorting required.
I first sorted like this. I am not sure how to move forward.
List<String> allStrings= new ArrayList<String>();
List<String> rankList= new ArrayList<String>();
List<String> stateList= new ArrayList<String>();
List<String> dateList= new ArrayList<String>();
List<String> rankListCopy = new ArrayList<String>(rankList.size());
rankListCopy.addAll(rankList);
Collections.sort(rankListCopy,Collections.reverseOrder());
for(String el: rankListCopy) {
System.out.println(el);
}
if(rankList.equals(rankListCopy)) {
System.out.println("CARDS ARE ARRANGED IN serial ORDER");
}
else{
System.out.println("CARDS ARE NOT ARRANGED IN serial ORDER"); }
Example:
String rank state date alphabetically
Europe 2 raw 2019-06-12T09:00:00Z
India 5 raw 2018-06-12T09:00:00Z
new york 5 boiled 2020-07-12T09:00:00Z
US 0 cooked 2020-06-12T09:00:00Z
china 0 cooked 2020-06-12T09:00:00Z
output:
Europe
newyork
india
china
US
Upvotes: 0
Views: 281
Reputation: 113
I'm not sure if that's what you need. But if you could represent that thing you are trying to sort as the collection of objects with properties, then you could do something like that. Either you can write the comparison logic you need in a Comparator, or make your class Comparable and override compareTo() method. Anyway here is a proposition with Comparator. A small trick is used, that a enum natural order is the order in which the enum elements are defined. In your case enum fields should be in that order: ("cooked","boiled","fresh","raw")
public class Main {
public static void main(String[] args) {
DateFormat df = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm'Z'");
String dateAsString = df.format(new Date());
List<String> allStrings = Arrays.asList("Europe", "India", "New York", "US", "China");
List<String> rankList = Arrays.asList("2", "5", "5", "0", "0");
List<String> stateList = Arrays.asList("RAW", "RAW", "BOILED", "COOKED", "COOKED");
List<String> dateList = Arrays.asList(dateAsString, dateAsString, dateAsString, dateAsString, dateAsString);
List<YourSortableEntity> inputData = new ArrayList<>();
for(int i =0; i < allStrings.size(); i++) {
try {
Date date = df.parse(dateList.get(i));
inputData.add(new YourSortableEntity(
allStrings.get(i),
Integer.valueOf(rankList.get(i)),
State.valueOf(stateList.get(i)),
date
));
} catch (ParseException e) {
throw new RuntimeException("Could not parse string into date: " + dateList.get(i), e);
}
}
Comparator<YourSortableEntity> comparator = Comparator.comparing(it -> it.getString());
comparator = comparator.thenComparing(it -> it.getRank()).thenComparing(it -> it.getState()).thenComparing(it -> it.getDate());
TreeSet<YourSortableEntity> treeSet = new TreeSet<>(comparator);
treeSet.addAll(inputData);
System.out.println(treeSet);
}
public static class YourSortableEntity {
private final String string;
private final int rank;
private final State state;
private final Date date;
public YourSortableEntity(String string, int rank, State state, Date date) {
this.string = string;
this.rank = rank;
this.state = state;
this.date = date;
}
public String getString() {
return string;
}
public int getRank() {
return rank;
}
public State getState() {
return state;
}
public Date getDate() {
return date;
}
@Override
public String toString() {
return string;
}
}
public enum State {
COOKED,
BOILED,
FRESH,
RAW
}
}
Upvotes: -1