Sunny
Sunny

Reputation: 928

Sort Object on the basis of parameter in java

I have a java Class SalesDataJson as shown below. The different status values are 'READY', 'PICKED' and 'PACKED'.

SalesDataJson.java

public class SalesDataJson {
    private Long salesOrderNumber;
    private String status;
 }

Now i want to order the 'SalesDataJson' object in the order of the status 'READY','PICKED' and 'PACKED'. Can anyone please help me with this.

I have the done the below code but it is not working:-

   public void sort(){
        Collections.sort(salesDataJsons, new Comparator<SalesDataJson>() {
            @Override
            public int compare(SalesDataJson o1, SalesDataJson o2) {
                if (o1.getStatus() == PackagingStatus.RTW && o2.getStatus() == PackagingStatus.PICKED)
                    return -1;
                else if (o1.getStatus() == PackagingStatus.PICKED && o2.getStatus() == PackagingStatus.RTW)
                    return 1;
                else if (o1.getStatus() == PackagingStatus.RTW && o2.getStatus() == PackagingStatus.PACKED)
                    return -1;
                else if (o1.getStatus() == PackagingStatus.PACKED && o2.getStatus() == PackagingStatus.RTW)
                    return 1;
                else if (o1.getStatus() == PackagingStatus.PICKED && o2.getStatus() == PackagingStatus.PACKED)
                    return -1;
                else if (o1.getStatus() == PackagingStatus.PACKED && o2.getStatus() == PackagingStatus.PICKED)
                    return 1;
                return 0;
            }
        });
    }

Upvotes: 2

Views: 67

Answers (3)

aUserHimself
aUserHimself

Reputation: 1597

If your PackagingStatus enum is defined in this way (ordered the way you like):

public enum PackagingStatus{
    RTW, PICKED, PACKED;
}

you could just use ordinal() which returns the position in the enumeration.

public void sort(){
    Collections.sort(salesDataJsons, new Comparator<SalesDataJson>() {
        @Override
        public int compare(SalesDataJson o1, SalesDataJson o2) { 
            return (o1.getStatus().ordinal() - o2.getStatus().ordinal());
        }
    });
}

Anyway, this is not really recommended since it is brittle, a small re-ordering could break your sorting.

You could attach this sorting logic in a helper class (as @Stephen Friedrich) suggested or directly into the enum itself (even if using a String status is again not ideal, I would replace it with PackagingStatus directly):

public enum PackagingStatus {
    PICKED(1), PACKED(2), RTW(3);

    PackagingStatus(int position) {
        this.position = position;
    }

    private final int position;

    public int getPosition() {
        return position;
    }
}

Then the sort becomes:

public void sort(){
    Collections.sort(salesDataJsons, new Comparator<SalesDataJson>() {
        @Override
        public int compare(SalesDataJson o1, SalesDataJson o2) { 
            return (o1.getStatus().getPosition() - o2.getStatus().getPosition());
        }
    });
}

Of course, if you'd like to write and call the sort using java8, this is the equivalent using lambda expressions:

Collections.sort(
    salesDataJsons,
    (o1, o2) ->
        o1.getStatus().getPosition() - o2.getStatus().getPosition()
);

Upvotes: 1

Schidu Luca
Schidu Luca

Reputation: 3947

You just arrange the statuses in the order you like and then your sorting comes easy as this:

List<SalesDataJson> sortedList = orignalList.stream()
               .sorted(Comparator.comparing(SalesDataJson::getStatus))
               .collect(Collectors.toList());

Upvotes: 0

eekboom
eekboom

Reputation: 5822

Make a helper method like this:

int getSortOrder(String status) {
    switch(status) {
        case PackagingStatus.RTW: 1; break;
        case PackagingStatus.PICKED: 2; break;
        case PackagingStatus.PACKED: 3; break;
        default:
            throw new RuntimeException("Unknown status" + status);
    }
}

then you can do

new Comparator<SalesDataJson>() {
    @Override
    public int compare(SalesDataJson o1, SalesDataJson o2) {
        return Integer.compare(getSortOrder(o1.getStatus), getSortOrder(o2.getStatus)));
    }
}

Upvotes: 1

Related Questions