javaarrayssortingcomparator

java comparator sort with model array and another field


I have a java class..

public class response_message {

    private String message;
    private String rejectCode;
    private int ruleSeqNo;
    private String contInd;

    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }

    public String getRejectCode() {
        return rejectCode;
    }

    public void setRejectCode(String rejectCode) {
        this.rejectCode = rejectCode;
    }

    public int getRuleSeqNo() {
        return ruleSeqNo;
    }

    public void setRuleSeqNo(int ruleSeqNo) {
        this.ruleSeqNo = ruleSeqNo;
    }

    public String getContInd() {
        return contInd;
    }

    public void setContInd(String contInd) {
        this.contInd = contInd;
    }

}

Then an object array of it as well as a string array

private List<response_message> responseMessage = new ArrayList<response_message>();

private String[] reqRejects;

public String[] getReqRejects() {
    return this.reqRejects;
}

public void setReqRejects(String[] reqRejects) {
    this.reqRejects = reqRejects;
}

My requirement is to sort this responseMessage object array with the model string array reqRejects and then with ruleSeqNo (ascending)

Eg. Input Object array

Message Reject Code Rule Seq No Cont Ind
abcd 60 101 Y
cdef 40 300 Y
xyz 41 280 Y
133 41 270 Y
56f 40 275 Y
677 60 102 Y
hfdd A5 400 Y

Model String array

Reject Code
A5
40
60
41

The response message object array has reject code column. And reqRejects string array (reject code) is the model array. So, basically, the object array needed to be first sorted on the reject code column in the same order as the reject code model string array is listed. And then on the rule seq no.

Output sorted Object array should be

Message Reject Code Rule Seq No Cont Ind
hfdd A5 400 Y
56f 40 275 Y
cdef 40 300 Y
abcd 60 101 Y
677 60 102 Y
133 41 270 Y
xyz 41 280 Y

Currently i have done with comparator as below which only sorts on the sequence number

responseMessage.sort(Comparator.comparingInt(m -> m.getRuleSeqNo()));

Thinking how to add the model string array based sort as well to this.


Solution

  • You can do it like this.

    List<String> recRejects = List.of("A5", "40", "60", "41");
    
    List<ResponseMessage> list = new ArrayList<>(
            List.of(new ResponseMessage("abcd", "60", 101, "Y"),
                    new ResponseMessage("cdef", "40", 300, "Y"),
                    new ResponseMessage("xyz", "41", 280, "Y"),
                    new ResponseMessage("133", "41", 270, "Y"),
                    new ResponseMessage("56f", "40", 275, "Y"),
                    new ResponseMessage("677", "60", 102, "Y"),
                    new ResponseMessage("hfdd", "A5", 400, "Y")));
    
    list.sort(Comparator
            .<ResponseMessage>comparingInt(msg -> recRejects.indexOf(msg.getRejectCode()))
            .thenComparing(ResponseMessage::getRuleSeqNo));
    
    list.forEach(System.out::println);
    

    Using the the following toString() method added to your class.

     @Override
     public String toString() {
            return "%10s %10s %10s %10s".formatted(message, rejectCode, ruleSeqNo,
                    contInd);
    

    Here are the results.

          hfdd         A5        400          Y
           56f         40        275          Y
          cdef         40        300          Y
          abcd         60        101          Y
           677         60        102          Y
           133         41        270          Y
           xyz         41        280          Y