This post shows how to sort an array or collection of Enum types in Java. We can use the natural sort order or sort by their properties. Any Enum type we create implements the Comparable interface. Below is an excerpt from the Java documentation.
Enum<E> implements Comparable<E> via the natural order of the enum (the order in which the values are declared).
Enum Type And Nature Order
Consider this Enum type with the following definition. We have three constants – HIGH, MEDIUM, and LOW. Each has a corresponding value for its level variable. Therefore, HIGH has 3, MEDIUM is 2, and LOW is 1.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | enum Value { HIGH(3), MEDIUM(2), LOW(1); private final int level; private Value(int level) { this.level = level; } public int getLevel() { return level; } } |
If we are to sort by constants’ names in ascending order, Java will use their natural order. Therefore, we will get HIGH, MEDIUM, and LOW because H comes before M which comes before L. What if we want to sort the Enum constants by their level properties? Read on.
Sorting Enum Constants To Their Natural Order
To demonstrate this default natural order for Enum constants, consider the following codes below. The codes use an array of Value and the Arrays.sort method. Without specifying additional parameters, we get HIGH, MEDIUM, and LOW, in that order.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | package com.turreta.ocp; import java.util.Arrays; public class EnumSortDemo01 { public static void main(String[] args) { Value[] val = {Value.LOW, Value.HIGH, Value.MEDIUM}; Arrays.sort(val); for(Value v: val) { System.out.println(v); } } } |
This outputs:
Sort By Enum Type Properties
On the other hand, we may need to sort the Enum constant by their properties. In order words, we don’t rely on the natural order of the constants’ names. Consider the following codes. They are similar to the previous codes. However, we are using the Comparator interface to compare the level variables.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | package com.turreta.ocp; import java.util.Arrays; import java.util.Comparator; public class EnumSortDemo02 { public static void main(String[] args) { Value[] val = {Value.LOW, Value.HIGH, Value.MEDIUM}; Arrays.sort(val, Comparator.comparing(Value::getLevel)); for(Value v: val) { System.out.println(v); } } } |
This outputs:
Sort Enum Type With Multiple Properties
In case we have an Enum type with multiple properties, we can still use the Comparator interface. This time, we chain together some of its methods. Consider the following codes – assuming we have the new gradeproperty.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | package com.turreta.ocp; import java.util.Arrays; import java.util.Comparator; public class EnumSortDemo02 { public static void main(String[] args) { Value[] val = {Value.LOW, Value.HIGH, Value.MEDIUM}; Arrays.sort(val, Comparator.comparing(Value::getLevel) .thenComparing(Value::getGrade)); for(Value v: val) { System.out.println(v); } } } |
Notice how we used the methods comparing and thenComparing methods.