Sorting of enum values in enumclass

Hi
I have written the following code but I am unable to sort the enum values in enum class.How can I sort it?Enum

Hi,

Could you clarify your needs? What do you mean by

unable to sort the enum values in enum class

Regards,
Gleb

Hi Gleb,

I want to sort the enum values in alphabetical order in enum class.

Hey!

Convert your enum to entity.
The names and the countries themselves are variable within a short time. Use enumerations for entities that do not change for a long time. For example, the sex of a person, the times of the year, the time of the day, the month, etc.
Bonus - sorting by any field of the entity as a gift :slight_smile:

I still do not understand your needs. Do you need to sort enum values in alphabetical order in fields like LookupField? Could you clarify in more detail?

Regards,
Gleb

Hi Gleb,

To sort the values in EnumCountry i am using the below code in the each controller wherever i am using this EnumCountry.

List<EnumCountry> countries = new ArrayList<>();
for (EnumCountry country : EnumCountry.values()) {
    countries.add(country);
}
countryField.setOptionsList(countries); // countryField is lookupField
countries.sort(Comparator.comparing(EnumCountry::name));

Instead of unsing this in every controller, can i get the sorting values directly from the EnumCountry.java file.

Now EnumCountry.java file is returning the values by id sorting. But I want to return the values by values Sorting.
Burkinafaso(4)
Ethiopia(1)
Ghana(5)
India(8)
Mali(6)
Nigeria(7)
Tanazania(2)
Uganda(3)

By default, the LookupFiled component sorts options according to the enum fields order of you use the setOptionsEnum method.

So, I suppose that the mentioned issue related to the way of adding options to the LookupField. How do you add options to the LookupField?

Regards,
Gleb

Hi Gleb,

To sort the values in EnumCountry i am using the below code in the each controller wherever i am using this EnumCountry.

Copy

List<EnumCountry> countries = new ArrayList<>();
for (EnumCountry country : EnumCountry.values()) {
    countries.add(country);
}
countryField.setOptionsList(countries); // countryField is lookupField
countries.sort(Comparator.comparing(EnumCountry::name));

Instead of using this in every controller, can i get the sorting values directly from the EnumCountry.java file.

The following code is used in EnumCountry.java

package com.keansa.keansacloud.entity;

import com.haulmont.chile.core.datatypes.impl.EnumClass;

import javax.annotation.Nullable;


public enum EnumCountry implements EnumClass<Integer> {

    Ethiopia(1),
    Tanzania(2),
    Uganda(3),
    Burkina_Faso(4),
    Ghana(5),
    Mali(6),
    Nigeria(7),
    India(8),
    Malawi(9),
    Mozambique(10);

    private Integer id;

    EnumCountry(Integer value) {
        this.id = value;
    }

    public Integer getId() {
        return id;
    }

    @Nullable
    public static EnumCountry fromId(Integer id) {
        for (EnumCountry at : EnumCountry.values()) {
            if (at.getId().equals(id)) {
                return at;
            }
        }
        return null;
    }
}

From above code the values are returning by id sorting,I want to sort the values by names of the country.
Is there any way to sort values in EnumCountry.Java

Hi @gorelov

In EditorController.java I am using below code to get alphabetical order of EnumValues

List countries = new ArrayList<>();
for (EnumCountry country : EnumCountry.values()) {
countries.add(country);
}
countryField.setOptionsList(countries); // countryField is lookupField
countries.sort(Comparator.comparing(EnumCountry::name));

Please find the attached image which is showing all the countries in alphabetical order correctly
country-editor-sorting

When I open the browser screen- in filters it is not showing in the alphabetical order
Please find the attached image

filter-dropdown-country-sort

Even while sorting the country in the browser when i set to Ascending it is not showing in the alphabetical order of enums
Please see the attached image in that for country field is showing first Mozambique, Malawi and India.

How to get the alphabetical order of enum values in the filter and browser.

Gleb, please help me here.

Thanks,
Akhil

Be more simple!
Why invent crutches and bicycles ?!
Convert your enum to entity.