Python Enum: Display all the member name of an enum class ordered by their values
3. Display Enum member names ordered by their values
Write a Python program to display all the member names of an enum class ordered by their values.
Sample Solution:
Python Code:
import enum
class Country(enum.IntEnum):
Afghanistan = 93
Albania = 355
Algeria = 213
Andorra = 376
Angola = 244
Antarctica = 672
print('Country Name ordered by Country Code:')
print('\n'.join(' ' + c.name for c in sorted(Country)))
Sample Output:
Country Name ordered by Country Code: Afghanistan Algeria Angola Albania Andorra Antarctica
Flowchart:

Visualize Python code execution:
The following tool visualize what the computer is doing step-by-step as it executes the said program:
For more Practice: Solve these Related Problems:
- Write a Python program to extract all member names from an Enum, sort them by their associated values, and print the sorted names.
- Write a Python program to use the sorted() function on an Enum’s members and then output only the names in order of increasing value.
- Write a Python program to implement a function that returns a list of member names from an Enum class sorted according to their numeric values.
- Write a Python program to iterate over an Enum and build a list of member names in order sorted by their values, then print the list.
Python Code Editor:
Contribute your code and comments through Disqus.
Previous: Write a Python program to iterate over an enum class and display individual member and their value.
Next: Write a Python program to get all values from an enum class.
What is the difficulty level of this exercise?
Test your Programming skills with w3resource's quiz.