Search code examples
javasortingdata-structurescollectionscomparable

Sorting List<Object> in custom order by value fetched from Enum


I have a class which is as follows:

public class Status implements Serializable, Comparable<Status>{
    private int requestStatus;

    @Column(name = "STATUS")
    public int getStatus() {
        return status;
    }

    public void setStatus(int status) {
        this.status = status;
    }
}

The value in status gets stored on the front end through an Enum as per the code which is as follows:

public enum StatusEnum {
    REQUESTED(0), CANCELED(-1), VALIDATED(10), ONGOING(20), FINISHED(30);

    private int intValue;

    private StatusEnum (int intValue) {
        this.intValue = intValue;
    }

    public int getIntValue() {
        return intValue;
    }

    public void setIntValue(int intValue) {
        this.intValue = intValue;
    }

    public String getName() {
        return this.name();
    }

    public static StatusEnum getStatusFromEnum(int code) {
        for (StatusEnum status : StatusEnum.values()) {
            if (status.getIntValue() == code) {
                return status;
            }
        }
        return null;
    }

    public static StatusEnum getStatusFromString(String name) {
        for (StatusEnum status : StatusEnum .values()) {
            if (status.name().equalsIgnoreCase(name)) {
                return status;
            }
        }
        return null;
    }

I am thereby willing to sort this list as per a custom order to display on the web part to carry out certain operations further. The order I want is as follows:

1: Requested (0) 2: Validated (10) 3: Ongoing(20) 4: Finished(30) 5: Cancelled (-1)

Can someone please help me with the sorting code to arrange this List<Status> status object? If someone can sort it as per the following order mentioned above to display it on the web end, it would be of great help.

Thanks in advance


Solution

  • I think what you need is the implement function of public int compareTo(Status o), and the sort method is:

    When the value is greater than 0, sort from smallest to largest, and when the value is less than 0, put it at the end of the list.

    Here is the implemented function:

    @Override
    public int compareTo(Status o) {
        //all >=0, sort by value 
        if (status >= 0 && o.status >= 0) {
            return Integer.compare(status, o.status);
        }
        // all <=0, equals
        if (status < 0 && o.status < 0) {
            return 0;
        }
        //one >=0, the other one <0, put the <0 value to the end of the list
        if (status < 0) {
            return 1;
        } else {
            return -1;
        }
    }
    

    then use the sort function Collections.sort(status) to sort is enough。 Here is my test:

    class Status implements Serializable, Comparable<Status> {
        private int status;
    
        public Status(int status) {
            this.status = status;
        }
    
        public int getStatus() {
            return status;
        }
    
        public void setStatus(int status) {
            this.status = status;
        }
    
        @Override
        public int compareTo(Status o) {
    //all >=0, sort by value
            if (status >= 0 && o.status >= 0) {
                return Integer.compare(status, o.status);
            }
    // all <=0, equals
            if (status < 0 && o.status < 0) {
                return 0;
            }
    //one >=0, the other one <0, put the <0 value to the end of the list
            if (status < 0) {
                return 1;
            } else {
                return -1;
            }
        }
    
        @Override
        public String toString() {
            return "Status{" +
                    "status=" + status +
                    '}';
        }
    }
    
    public static void main(String[] args) {
        Status REQUESTED = new Status(0);
        Status CANCELED = new Status(-1);
        Status VALIDATED = new Status(10);
        Status ONGOING = new Status(20);
        Status FINISHED = new Status(30);
        List<Status> statusList = Arrays.asList(REQUESTED, CANCELED, VALIDATED, ONGOING, FINISHED);
        Collections.sort(statusList);
        //prints
        //[Status{status=0}, Status{status=10}, Status{status=20}, Status{status=30}, Status{status=-1}]
        System.out.println(statusList);
    }