0

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.

Hovercraft Full Of Eels
286k25 gold badges267 silver badges391 bronze badges
asked Mar 12, 2016 at 18:11
5
  • 2
    you must implement comparator for your class GetTraders. Commented Mar 12, 2016 at 18:14
  • 1
    Implement a Comparator Commented Mar 12, 2016 at 18:15
  • 1
    Please 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. Commented Mar 12, 2016 at 18:23
  • How should they be sorted? By name? Businessname? Commented Mar 12, 2016 at 18:24
  • I just want to sort based on businessname, and getTraderLegalName. Commented Mar 12, 2016 at 19:29

4 Answers 4

7

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);
 }
}
answered Mar 12, 2016 at 18:29

2 Comments

Is it possible to add one more condition here? like sorting based on getTradelegalname, and businessName.
If you now want to sort by "getTradelegalname" and, in case of equality, then sort by "businessName" you can do it easily. In the Comparable (or Comparator) logic, check the result of o1.getTraderLegalName.compareTo(o2.getTraderLegalName), if it is != 0 then return it, otherwise return the result of o1.businessName.compareTo(o2.businessName). After you master these interfaces, have a look at how you could implement it in Java 8 using the new "comparing" methods of the Comparator interface. It is more elegant and concise.
1

here is another way to sort list elements based on businessName:

 traders.sort((GetTraders trade1, GetTraders trade2) -> {
 return trade1.getBusinessName().compareTo(trade2.getBusinessName());
 });
answered Mar 12, 2016 at 18:37

Comments

0

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
answered Mar 12, 2016 at 19:21

Comments

-4

Use Comparator to determine order.

Collections.sort(someList, comparator);
trincot
356k37 gold badges278 silver badges337 bronze badges
answered Mar 12, 2016 at 18:17

1 Comment

Could you expand a little, and maybe explain / describe how Comparator can be used ?

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.