0

I have list of objects and structure of the object is defined below:

class PredicateInfo {
    String predicateName;
    String predicateStatus;
}

Here, predicateName can be any valid string and predicateStatus can be any one of the string from these values: VERIFIED, IN_PROGRESS, UNVERIFIED, NOT_INITIATED.

Priority of these strings: 
Priority 1: VERIFIED
Priority 2: IN_PROGRESS
Priority 3: UNVERIFIED
Priority 4: NOT_INITIATED

Here I have a use case where I want to sort List[PredicateInfo] based on the predicateStatus. For ex:

Input list:
List[ PredicateInfo("A", "IN_PROGRESS"), PredicateInfo("A", "VERIFIED")]
Output:
List[ PredicateInfo("A", "VERIFIED"), PredicateInfo("A", "IN_PROGRESS")]

One simple solutions is to iterate over and over to get the sorted list, I am trying to find other alternatives to achieve the same.

Jhutan Debnath
  • 505
  • 3
  • 13
  • 24

3 Answers3

0

Use Java's Comparator to achieve this:

public Comparator<PredicateInfo> PredicateInfoComparator 
                      = new Comparator<PredicateInfo>() {

    public int compare(PredicateInfo info1, PredicateInfo info2) {

      //your sorting logic here
    }

};

And calling it using:

Collections.sort(list, new PredicateInfoComparator());

As explained by Javadoc, the sorting logic should return a negative integer, zero, or a positive integer as the first argument is less than, equal to, or greater than the second. See here for complete Javadoc.

Alternatively, PredicateInfo can implement Comparable interface and you can call sorting calling:

Collections.sort(list);

which will implicitly call the method compareTo declared in Comparable. More details here.

Lorelorelore
  • 3,335
  • 8
  • 29
  • 40
0

You can pass a comparator to the sort method.

List<String> predicateStatuses = new ArrayList<>();
predicateStatuses.add("VERIFIED");
predicateStatuses.add("IN_PROGRESS");
predicateStatuses.add("UNVERIFIED");
predicateStatuses.add("NOT_INITIATED");


predicateInfos.sort(Comparator.<PredicateInfo>comparingInt(predicateInfo -> predicateStatuses.indexOf(predicateInfo.getPredicateStatus()))
            .thenComparing(PredicateInfo::getPredicateName));

The logic of the comparator function is:

First, sort by the position of the predicateStatus string in the predicateStatuses list. This is the order (or priority) you have provided. So, the PredicateInfo object with predicateStatus = VERIFIED will come first in the output.

Next, for objects with same predicateStatus sort by the natural ordering (lexicographic) of the predicateName

Ideone Demo

Thiyagu
  • 17,362
  • 5
  • 42
  • 79
0

For values in a list :

Map<Integer, String> priorityMap = new HashMap<>();
priorityMap.put(1, "VERIFIED");
priorityMap.put(2, "IN_PROGRESS");
priorityMap.put(3, "UNVERIFIED");
priorityMap.put(4, "NOT_INITIATED");

Collections.sort(inputList, new Comparator<PredicateInfo >() {
      @Override
      public int compare(PredicateInfo obj1, PredicateInfo obj2) {
        return priorityMap.get(obj1.getPredicateStatus()) - priorityMap.get(obj2.getPredicateStatus())
      }
    });

This will give you a sorted list

  • The priority list should be: Map priorityMap = new HashMap<>(); priorityMap.put("VERIFIED", 1); priorityMap.put("IN_PROGRESS", 2); priorityMap.put("UNVERIFIED", 3); priorityMap.put("NOT_INITIATED", 4); Because HashMap.get() will return the value and in the compare() you use the value from the map as the priority for comparing each of them and by "predicate status" the object had. – Cuong Vo Jan 10 '21 at 18:15