Order List in java based on custom field value


Order List in java based on custom field value



I have a list as follows:


List<Data> list = new ArrayList<Data>()

Data {
String status;
String type;
}



From some Service I am getting data as below:


list = ["S","a"],["V","b"],["E","c"];



I need list to be ordered by status so that V,S,E so that I can pass it to filter for further processing:


updatedlist = ["V","a"],["E","b"],["S","c"];



I got understanding using compareTo we can arrange in ascending/descending order as follows but I dont need this


public int compare(Data d1, Data d2) {

String status1= d1.getStatus();
String status2= d2.getStatus();

//ascending order
return status1.compareTo(status2);

//descending order
//return status2.compareTo(status1);
}



Please suggest how can achieve the List status order as V ,E,S.





Isn't V-S-E just a descending order of the status? If not, can you explain the logic you want to use?
– Mureinik
Jun 29 at 10:53





is descending order not work for you?
– lucumt
Jun 29 at 10:53





Sorry for that. I updated the question.
– Manan Kapoor
Jun 29 at 10:57




3 Answers
3



You can define a list which specifies the order and then use indexOf and simple subtraction:


indexOf


private static final List<String> statusOrder = Arrays.asList("V", "S", "E");

public int compare(Data d1, Data d2) {
return statusOrder.indexOf(d1.getStatus()) - statusOrder.indexOf(d2.getStatus());
}



Be careful if the statuses may contain values not in the list, as indexOf will return -1. You should probably define Status as an enum to give yourself stronger guarantees.


indexOf


Status





I updated question. sorry for that.
– Manan Kapoor
Jun 29 at 10:58



Looks like you want to sort via type. If so use below approach:


list.stream().sorted(Comparator.comparing(Data::getType)).collect(Collectors.toList());



or without Lambda:


Collections.sort(list, new Comparator<Data>() {
@Override
public int compare(Data o1, Data o2) {
return o1.getType().compareTo(o2.getType());
}
});



it'll return in order ["V","a"],["E","b"],["S","c"]


["V","a"],["E","b"],["S","c"]





that is fine but I wanted to do on status.
– Manan Kapoor
Jun 29 at 12:29





@MananKapoor your question is unclear... how does ["S","a"] become ["S","c"]. For status Michael's answer is correct.
– Shubhendu Pramanik
Jun 29 at 12:36



["S","a"]


["S","c"]



What you could do is define your Status String in an Enum, then you can sort them as you put them there.


List<Data> data = ...
public enum Status {
V, E, S
}
public class Data {
private Status status;
private String type;
}
data.sort(Comparator.comparing(Data::getStatus));



The list is sorted like this now [V, b][E, c][S, a]


[V, b][E, c][S, a]



For Java6 replace the sort line with


data.sort(new Comparator<Data>() {
@Override
public int compare(Data o1, Data o2) {
return o1.getStatus().compareTo(o2.getStatus());
}
});





I am using Java6. I think this is in Java8
– Manan Kapoor
Jun 29 at 11:43





I edited the post with Java6 compatible code. Don´t need to change much.
– Max
Jun 29 at 13:14





That way you dont have to implement your own comperator. Otherwise id probably create an array and use the idexes for ordering (but then you need to loop threw the array and compare every string until you find the one). Hashmap would solve that map the string as key to int values for sorting priority. Then you compare those return -1 for smaller 0 for equal and +1 for bigger. Exactly how much negative or positive the number you re returning is mostly wont make any difference. Sure wont for the outcome, might for performance of the sorting algorythm. Would have to look into that too.
– Max
Jun 29 at 13:40






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Comments

Popular posts from this blog

paramiko-expect timeout is happening after executing the command

how to run turtle graphics in Colaboratory

Export result set on Dbeaver to CSV