Enum
Sorting Order
Ascending Descending
Programming Concepts
Software Development

Does an Enum exist for Asc or Desc ordering?

Master System Design with Codemia

Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.

Introduction

Whether an enum exists for ascending and descending order depends on the language and API you are using. The more important design question is usually not "is there a built-in enum somewhere," but "should sorting direction be modeled as a named type instead of magic strings or booleans."

When a Built-In Enum Exists

Some platforms do provide one. In .NET, a well-known example is System.ComponentModel.ListSortDirection, which exposes Ascending and Descending.

csharp
1using System;
2using System.ComponentModel;
3
4class Program
5{
6    static void Main()
7    {
8        ListSortDirection direction = ListSortDirection.Descending;
9        Console.WriteLine(direction);
10    }
11}

This compiles and runs as-is in a simple console app. If your code already uses APIs that accept ListSortDirection, use it. Reusing the type already expected by the framework is cleaner than creating your own duplicate enum.

There Is No Universal Sorting Enum

The important limitation is that there is no single cross-language, cross-library enum that all sorting APIs recognize. SQL uses keywords such as ASC and DESC. LINQ often uses method choice instead of a direction parameter, for example OrderBy versus OrderByDescending. Many libraries define their own type names if they need one.

That means the correct answer is usually:

  • use the framework enum if the API already exposes one
  • otherwise define a small enum in your own code if direction is part of your domain model

Why an Enum Is Better Than a Boolean

A boolean such as true for descending and false for ascending looks compact, but it is ambiguous at the call site. An enum makes the intent obvious.

Compare the two approaches:

csharp
SortUsers(users, true);

Versus:

csharp
SortUsers(users, SortDirection.Descending);

The second call explains itself. That matters once the code is reviewed months later or the method gains additional sorting options.

Defining Your Own Enum

If the platform does not expose a suitable type, defining your own is trivial and usually the right move.

csharp
1using System;
2using System.Collections.Generic;
3using System.Linq;
4
5enum SortDirection
6{
7    Ascending,
8    Descending
9}
10
11class Program
12{
13    static IEnumerable<int> SortNumbers(IEnumerable<int> numbers, SortDirection direction)
14    {
15        return direction == SortDirection.Ascending
16            ? numbers.OrderBy(n => n)
17            : numbers.OrderByDescending(n => n);
18    }
19
20    static void Main()
21    {
22        var values = new[] { 4, 1, 3, 2 };
23        foreach (var value in SortNumbers(values, SortDirection.Descending))
24        {
25            Console.WriteLine(value);
26        }
27    }
28}

This keeps invalid values out of the API and makes future changes easier than using strings such as "asc" and "desc".

Matching the Enum to the Real Abstraction

Do not force a direction enum into every sorting API. Sometimes the abstraction is different.

For example, a query builder might expose separate methods:

csharp
query = query.OrderBy(x => x.Name);
query = query.OrderByDescending(x => x.Name);

In that style, direction is represented by method choice, not by data. Wrapping that API in a direction enum can still be useful if your application takes the sort order from user input, but the underlying library does not need an enum to be valid.

Parsing User Input Safely

If direction comes from a query string or UI selection, convert the raw value at the boundary and use the enum internally.

csharp
1using System;
2
3enum SortDirection
4{
5    Ascending,
6    Descending
7}
8
9class Program
10{
11    static bool TryParseDirection(string input, out SortDirection direction)
12    {
13        return Enum.TryParse(input, ignoreCase: true, out direction);
14    }
15
16    static void Main()
17    {
18        if (TryParseDirection("descending", out var direction))
19        {
20            Console.WriteLine(direction == SortDirection.Descending ? "desc" : "asc");
21        }
22    }
23}

That is safer than passing raw strings through the application and hoping every caller spells them correctly.

Common Pitfalls

The most common mistake is assuming there must be one built-in enum for all cases. In reality, sort direction is modeled differently across frameworks.

Another mistake is using a boolean because it feels shorter. Shorter source code is not better when it makes the method call ambiguous.

Developers also create a custom enum even when the framework already defines one accepted by the target API. That adds unnecessary conversion code.

Finally, avoid storing unvalidated strings such as "ascending" in core business logic. Parse them once, then use a strongly typed enum or the library's own sorting API.

Summary

  • Some frameworks provide a sorting-direction enum, but there is no universal built-in enum across all languages and libraries.
  • In .NET, ListSortDirection is a common built-in option.
  • If no suitable type exists, defining your own Ascending and Descending enum is a good design choice.
  • Enums communicate intent better than booleans or raw strings.
  • Prefer the abstraction used by the actual API: framework enum, custom enum, or method choice.

Course illustration
Course illustration

All Rights Reserved.