0

My service is returning a page of elements, and I want to be able to sort it by many fields. So far I can sort it by all fields except this enumeration field:

enum ActionStatusEnum {
    TO_DO("ToDo"),
    ROUGH_DRAFT("Brouillon"),
    WAITING_FOR_VALIDATION("En attente de validation"),
    VALIDATED("Validée"),
    ......

    private String label;

    ActionStatusEnum(String value) {
        label = value;
    }

    public String getLabel() {
        return label;
    }

So far my sort looks like that (status being the enumeration field)

for (String sort : sorts) {
            switch(sort) {
                case "createDate":
                    s = new Sort(Sort.Direction.DESC, "creationDate");
                    mySort = mySort == null ? s : mySort.and(s);
                    break;
                case "rubric":
                    s = new Sort(Sort.Direction.ASC, "task.rubric");
                    mySort = mySort == null ? s : mySort.and(s);
                    break;

                ........ // other cases

                case "status":
                    s = new Sort(Sort.Direction.DESC, "status");
                    break;
                default :
                    s = new Sort(Sort.Direction.DESC, "submissionDate");
                    mySort = mySort == null ? s : mySort.and(s);
                    break;
            }
        }

As you expect whenever I sort by status, it gets sorted by alphabetic order; while I need to order it as it is declared in the enumeration (TO_DO then ROUGH_DRAFT then WAITING_FOR_VALIDATION ....) . Is there any solution to that, like making my custom enumeration ordering or something that way. Thank you in advance

  • 1
    Where do you read values from ? DB, File or any kind of other memories ? Maybe you can think of the receive values already ordered besides sorting here ? – Hayra Oct 08 '19 at 11:17
  • @Hayra from DB, but i can't just get pre-ordered values, cause the user is the one to choose wich sorts to use and their order of using, like he can choose to order by date then status... – Amine Benhammou Oct 08 '19 at 11:24

0 Answers0