Search code examples
javasortingenumscomparatorcomparable

Re-order two values from an alphabetically ordered list. Enum/Comparataor


coded list of values A,B,C,D,E,F,G.... I want D to be placed before C while iterating through the list in the JSP.

I'm open for enum or to use comparators.

Currently the enum SortOrder is different object from the Comparable which i currently have.

So how can i deal with this below code.

//override

    public int compareTo(Alphabets) {

        return name.toLowerCase().compareTo(o.getName().toLowerCase());
      }

I've tried

private SortOrder so
     public void setName(String name) {
        this.name = so.values().toString();
      }

But it throws "The static method values() from the type SortOrder should be accessed in a static way"

Please help me.

I've tried stackoverflow and found this, which is close, but not enough: Java Sorting based on Enum constants

Edit: sorry to confuse everyone, the above question is just a representation of what my issue is. The words which i have for A,B,C,.. have a different presentation in my code. I have a list of brand names for a product, where i want to re-order the list.


Solution

  • I'm still not quite sure I understand your question, but if I do, here are two solutions:

    With enum

    Smallest solution, limited to possible names for enum values. demo

    enum SortOrderEnum {
        A, B, D, C, E, F;
    }
    

    Sort with

    Collections.sort(testData, new Comparator<String>() {
        @Override
        public int compare(String s1, String s2) {
            return SortOrderEnum.valueOf(s1).compareTo(SortOrderEnum.valueOf(s2));
        }
    });
    

    Without enum

    You can put any string you want, I didn't see the point of using an enum in this solution. We put the expected index for each string in a static map. demo

    public class SortOrder {
        public static final Map<String, Integer> SORT_ORDER = initializeSortOrder();
    
        private static Map<String, Integer> initializeSortOrder() {
            List<String> orderedStrings = Arrays.asList(
                    "A",
                    "B",
                    "D",
                    "C",
                    "E",
                    "F"
            );
            Map<String, Integer> indexByString = new HashMap<>();
            for (int index = 0; index < orderedStrings.size(); ++index) {
                indexByString.put(orderedStrings.get(index), index);
            }
            return Collections.unmodifiableMap(indexByString);
        }
    }
    

    Sort with

    Collections.sort(testData, new Comparator<String>() {
        @Override
        public int compare(String s1, String s2) {
            return SORT_ORDER.get(s1).compareTo(SORT_ORDER.get(s2));
        }
    });
    

    Note that this is an example of how to fill the map. You can (better) fill the string->index map from a configuration file.

    You can also not put the map in a static variable if that causes problems for you.