-1

I have a list of MyObject

List<MyObject> myObjects;

where MyObject is a model class something like below

public class MyObject{

private String fName;
private String lname;
private String code;

//getter setter

}

there are four possible value of code let's say ABC,DEF,XYZ and PQR.

Now i want to sort the list based on following criteria. All object with code value XYZ should come first, followed by PQR,ABC and DEF.

  1. XYZ
  2. PQR
  3. ABC
  4. DEF

I want to achieve this using java 8 if possible. How can i sort my ArrayList.

user9735824
  • 1,196
  • 5
  • 19
  • 36
  • 2
    Use a custom `Comparator`. [How to sort List of objects by some property](https://stackoverflow.com/questions/5805602/how-to-sort-list-of-objects-by-some-property) would be a good place to start – MadProgrammer Oct 09 '19 at 21:22
  • i figured that part but was not achieve to make it work. – user9735824 Oct 09 '19 at 21:25
  • If you want help, you must show what you tried. – JB Nizet Oct 09 '19 at 21:28
  • 1
    `myObjects.sort((o1, o2) -> { if(o1.getCode().equals("XYZ")) return -1; else if(o2.getCode().equals("XYZ")) return 1; // else return o1.getCode().compareTo(o2.getCode()); });` – pvpkiran Oct 09 '19 at 21:39
  • 1
    Remember, you're comparing the keys of two objects and how the relate to each other – MadProgrammer Oct 09 '19 at 21:41
  • 1
    assign a numeric value to each code: XYZ -> 1, PQR -> 2, ABC -> 3, DEF -> 4. Get the numeric value for o1, and the one for o2. Compare the numeric values. – JB Nizet Oct 09 '19 at 21:44

2 Answers2

4

If there are only four possible values that your code variable can take, you could save them in a map and compare the values when sorting your list:

public static void main(String[] args) {             
    List<MyObject> myObjects = new ArrayList<>();
    myObjects.add(new MyObject("fName1", "lname1", "ABC"));
    myObjects.add(new MyObject("fName2", "lname2", "PQR"));
    myObjects.add(new MyObject("fName3", "lname3", "XYZ"));
    myObjects.add(new MyObject("fName4", "lname4", "DEF"));

    Map<String,Integer> map = new HashMap<>();
    map.put("XYZ", 1);
    map.put("PQR", 2);
    map.put("ABC", 3);
    map.put("DEF", 4);

    Comparator<MyObject> sortByCode = (obj1,obj2)->Integer.compare(map.get(obj1.code), map.get(obj2.code));
    System.out.println("Before sorting");
    System.out.println(myObjects);

    System.out.println("After sorting");
    myObjects.sort(sortByCode);
    System.out.println(myObjects);
}
Eritrean
  • 15,851
  • 3
  • 22
  • 28
1

You'd need to create your own Comparator for comparing instances of MyObject according to your logic:

Comparator<MyObject> cmp = (o1, o2) ->{
    //Implement comparison logic here
    //Compare o1 and o2 and return -1,0, or 1 depending on your logic
};

Then given a list such as this:

List<MyObject> listToSort = ...

You can either sort it in-place using the old Collections.sort() function:

Collections.sort(listToSort, cmp);

Or, if you want, using Java 8 streams:

listToSort.stream().sorted(cmp).collect(Collectors.toList()); //Using streams
Malt
  • 28,965
  • 9
  • 65
  • 105