简体   繁体   中英

How do I sort enum members alphabetically in Java?

I have an enum class like the following:

public enum Letter {
    OMEGA_LETTER("Omega"), 
    GAMMA_LETTER("Gamma"), 
    BETA_LETTER("Beta"), 
    ALPHA_LETTER("Alpha"), 

    private final String description;

    Letter() {
      description = toString();
    }

    Letter(String description) {
      this.description = description;
    }

    public String getDescription() {
      return description;
    }
}

Later down my code I basically iterate over the Letter enum and print its members out to the console:

for (Letter letter : Letter.values()) {
System.out.println(letter.getDescription());
}

I thought that the values() method would give me an ordered view of the enum (as mentioned here ), but this is not the case here. I simply get the enum members in the order I created them within the Letter enum class. Is there a way to output the values of an enum in alphabetical order? Would I need a separate comparator object, or is there a built-in way to do this? Basically I would like the values to be alphabetically sorted based on the getDescription() text:

Alpha
Beta
Gamma
Omega
SortedMap<String, Letter> map = new TreeMap<String, Letter>();
for (Letter l : Letter.values()) {
    map.put(l.getDescription, l);
}
return map.values();

Or just reorder the declarations :-)

Edit: As KLE pointed out, this assumes that the Descriptions are unique within the enum.

I thought that the values() method would give me an ordered view of the enum (as mentioned here), but this is not the case here. I simply get the enum members in the order I created them within the Letter enum class.

Precisely, the order of declaration is considered significant for enums, so we are glad that they are returned in precisely that order. For example, when a int i represents an enum values, doing values()[i] is a very simple and efficient way to find the enum instance. To go contrary-wise, the ordinal() method returns the index of an enum instance.

Is there a way to output the values of an enum in alphabetical order? Would I need a separate comparator object, or is there a built-in way to do this? Basically I would like the values to be alphabetically sorted based on the getDescription() text:

What you call value is not something defined for enums in general. Here, in your context, you mean the result of getDescription() .

As you say, you could create a Comparator for these descriptions . That would be perfect :-)


Note that in general, you could need several orders for these instances :

  • declaration order (this is the official order)
  • description order
  • others as needed

You could also push that notion of DescriptionComparator a little bit:

  1. For performance reasons, you could store the computed descriptions.

  2. Because enums can't inherit, code reuse has to be outside the enum class. Let me give the example we would use in our projects:

Now the code samples...

/** Interface for enums that have a description. */
public interface Described {
  /** Returns the description. */
  String getDescription();
}

public enum Letter implements Described {
  // .... implementation as in the original post, 
  // as the method is already implemented
}

public enum Other implements Described {
  // .... same
}

/** Utilities for enums. */
public abstract class EnumUtils {

  /** Reusable Comparator instance for Described objects. */
  public static Comparator<Described> DESCRIPTION_COMPARATOR = 
    new Comparator<Described>() {
      public int compareTo(Described a, Described b) {
        return a.getDescription().compareTo(b.getDescription);
      }
    };

  /** Return the sorted descriptions for the enum. */
  public static <E extends Enum & Described> List<String> 
    getSortedDescriptions(Class<E> enumClass) {
      List<String> descriptions = new ArrayList<String>();
      for(E e : enumClass.getEnumConstants()) {
        result.add(e.getDescription());
      }
      Collections.sort(descriptions);
      return descriptions;
  }
}

// caller code
List<String> letters = EnumUtils.getSortedDescriptions(Letter.class);
List<String> others = EnumUtils.getSortedDescriptions(Other.class);

Note that the generic code in EnumUtils works not only for one enum class, but works for any enum class in your project that implements the Described interface .

As said before, the point of having the code outside of the enums (where it would otherwise belong) is to reuse the code. It's not big deal for two enums, but we have over a thousand enums in our project, many of them with the same interfaces...!

只需使用 Arrays.sort 和您自己的比较器对它们进行排序。

Here's a generic way to do it with any class without having to implement Comparable on the class you're sorting or create a custom comparator. I've found instances where I don't want to override compareTo because it serves a different purpose, you can't for enums anyways, and constantly creating wrapper classes is a pain. You can pass in a function that outputs a Comparable object you'd like to use for sorting purposes.

The toComparable function is only called once per element in the list (not so for a custom comparator), so it's especially good if that call is expensive for some class. Null values are handled internally, so it's easier to use than a custom comparator. One call to Java 7's TimSort algorithm is drastically more efficient than doing a bunch of O(log N) inserts to a SortedMap (red-black tree or other balanced tree implementation). And you aren't restricted to any particular class or interface.

Real world performance increases are significant in many cases. For example, the performance increase is around 5x as fast as using a comparator when sorting Doubles using toString() on a list of size 100k.

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.ListIterator;

public class GenericLetterSorter {
    public enum Letter {
        OMEGA_LETTER("Omega"), 
        GAMMA_LETTER("Gamma"), 
        BETA_LETTER("Beta"), 
        ALPHA_LETTER("Alpha"); 

        private final String description;

        Letter() {
          description = toString();
        }

        Letter(String description) {
          this.description = description;
        }

        public String getDescription() {
          return description;
        }
    }

public static void main(String[] args) {
    List<Letter> list = new ArrayList<>(Arrays.asList(Letter.values()));

    sort(list, new ToComparable<Letter>() {
        @Override
        public Comparable toComparable(Letter letter) {
            // sort based on the letter's description
            return letter == null ? null : letter.getDescription();
        }
    });

    for (Letter letter : list)
        System.out.println(letter == null ? null : letter.name());
}

    public interface ToComparable<T, C extends Comparable<? super C>> {
         C toComparable(T t);
    }

    public static <T, C extends Comparable<? super C>> void sort(List<T> list, ToComparable<T, C> function) {
       class Pair implements Comparable<Pair> {
          final T original;
          final C comparable;

          Pair(T original, C comparable) {
             this.original = original;
             this.comparable = comparable;
          }

          @Override
          public int compareTo(Pair other) {
                return
                  comparable == null && other.comparable == null ? 0 :
                  comparable == null ? -1 :
                  other.comparable == null ? 1 :
                  comparable.compareTo(other.comparable);
          }
       }

       List<Pair> pairs = new ArrayList<>(list.size());
       for (T original : list)
          pairs.add(new Pair(original, function.toComparable(original)));

       Collections.sort(pairs);

       ListIterator<T> iter = list.listIterator();
       for (Pair pair : pairs) {
          iter.next();
          iter.set(pair.original);
       }
    }
}

You can use sorted function with comparator in java8

enumlist.stream()
.sorted(Comparator.comparing(Enum::toString)).collect(Collectors.toList());

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM