Basically sorting could be done by following:
Collections.sort(List);
But my scenario is little bit different. I have a List which contains following objects.
Sample Code 1:
public class GetTraders {
private String getTraderLegalName;
private String businessName;
private Object status;
public GetTraders(String getTraderLegalName, String businessName, String status) {
this.getTraderLegalName=getTraderLegalName;
this.businessName=businessName;
this.status=status;
}
I have a class which will give value for above list as follow:
public Class getResult {
List<GetTraders> traders=new ArrayList<GetTraders>();
public void valueGivenByuser(String legal,String business,Object status)throws Exception {
GetTraders trade=new GetTraders(legal,business,status);
traders.add(trade); //Adding value to arrayList
}
}
The problem here is, once I added all values in traders arraylist I need to sort and display as output. I tried with collections.sort() but It shows compiler exception.
-
2you must implement comparator for your class GetTraders.waxtah– waxtah2016年03月12日 18:14:31 +00:00Commented Mar 12, 2016 at 18:14
-
1Implement a ComparatorIdos– Idos2016年03月12日 18:15:03 +00:00Commented Mar 12, 2016 at 18:15
-
1Please don't post poorly formatted code. Code formatting is there for a reason -- so that others can easily read and understand your code. If you post code without indentations, no one will bother reading it. I've fixed your code for you for now, but in the future, please put in this effort yourself.Hovercraft Full Of Eels– Hovercraft Full Of Eels2016年03月12日 18:23:26 +00:00Commented Mar 12, 2016 at 18:23
-
How should they be sorted? By name? Businessname?Yassin Hajaj– Yassin Hajaj2016年03月12日 18:24:24 +00:00Commented Mar 12, 2016 at 18:24
-
I just want to sort based on businessname, and getTraderLegalName.ArrchanaMohan– ArrchanaMohan2016年03月12日 19:29:57 +00:00Commented Mar 12, 2016 at 19:29
4 Answers 4
If you look closely to the Collections API, you will see that you have two options at your disposal:
1) make your GetTraders class implement the Comparable interface and call
public static <T extends Comparable<? super T>> void sort(List<T> list)
2) create a new Comparator for the GetTraders class and call
public static <T> void sort(List<T> list, Comparator<? super T> c)
The first solution is the easiest one but if you need to sort the GetTraders objects according to multiple criteria then the second one is the best choice.
As pointed out by @Vaseph, if you are using Java 8 instead, life suddenly becomes easier because all you need to do is:
traders.sort((GetTraders trade1, GetTraders trade2) -> {
return trade1.getBusinessName().compareTo(trade2.getBusinessName());
});
But if you are having troubles with the Comparable and Comparator interfaces, I would encourage you to first try the pre-Java-8 solutions before diving into the magic world of the functional interfaces.
For the sake of completeness, please also find below an example of each solution:
1) Comparable-based solution:
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
public class GetTraders1 implements Comparable<GetTraders1> {
private String getTraderLegalName;
private String businessName;
private Object status;
public GetTraders1(String getTraderLegalName, String businessName, String status) {
this.getTraderLegalName=getTraderLegalName;
this.businessName=businessName;
this.status=status;
}
@Override
public int compareTo(GetTraders1 that) {
return this.getTraderLegalName.compareTo(that.getTraderLegalName);
}
@Override
public String toString() {
return "GetTraders [getTraderLegalName=" + getTraderLegalName + ", businessName=" + businessName + ", status=" + status + "]";
}
public static void main(String[] args) {
GetTraders1 getTraders1 = new GetTraders1("1", "bn", "status");
GetTraders1 getTraders2 = new GetTraders1("2", "bn", "status");
GetTraders1 getTraders3 = new GetTraders1("3", "bn", "status");
List<GetTraders1> list = new ArrayList<>();
list.add(getTraders3);
list.add(getTraders2);
list.add(getTraders1);
System.out.println(list);
Collections.sort(list);
System.out.println(list);
}
}
2) Comparator-based solution
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
public class GetTraders2 {
private String getTraderLegalName;
private String businessName;
private Object status;
public GetTraders2(String getTraderLegalName, String businessName, String status) {
this.getTraderLegalName=getTraderLegalName;
this.businessName=businessName;
this.status=status;
}
@Override
public String toString() {
return "GetTraders [getTraderLegalName=" + getTraderLegalName + ", businessName=" + businessName + ", status=" + status + "]";
}
public static void main(String[] args) {
GetTraders2 getTraders1 = new GetTraders2("1", "bn", "status");
GetTraders2 getTraders2 = new GetTraders2("2", "bn", "status");
GetTraders2 getTraders3 = new GetTraders2("3", "bn", "status");
List<GetTraders2> list = new ArrayList<>();
list.add(getTraders3);
list.add(getTraders2);
list.add(getTraders1);
System.out.println(list);
Collections.sort(list, new Comparator<GetTraders2>() {
@Override
public int compare(GetTraders2 o1, GetTraders2 o2) {
return o1.getTraderLegalName.compareTo(o2.getTraderLegalName);
}
});
System.out.println(list);
}
}
2 Comments
here is another way to sort list elements based on businessName:
traders.sort((GetTraders trade1, GetTraders trade2) -> {
return trade1.getBusinessName().compareTo(trade2.getBusinessName());
});
Comments
This hasn't be proposed but here it is.
You should use Collections#sort
with a Comparator
Collections.sort(traders, (x, y) -> x.getXXX().compareTo(y.getXXX()));
// XXX could be any String object's getter
Comments
Use Comparator
to determine order.
Collections.sort(someList, comparator);
1 Comment
Comparator
can be used ?