---
title: "Custom Alphabetical Order — A C# Coding Challenge"
description: "Check if a list of words is sorted according to a custom alphabet. Problem breakdown and a C# solution."
canonical_url: "https://isiahjones.dev/blog/custom-alphabetical-order-csharp"
---

> Index of every page on this site: [llms.txt](https://isiahjones.dev/llms.txt)

Here's a fun challenge: you're given a **list of words** and a **custom alphabetical order** (e.g. not necessarily `a–z`). Your job is to write a function that returns whether the words are sorted according to that custom order.

## The problem

- **Input:** An array (or list) of words, and a string (or sequence) defining the custom order of characters.
- **Output:** `true` if the words are in non-descending order according to the custom alphabet; otherwise `false`.

For example, if the custom order is `"zyxwvutsrqponmlkjihgfedcba"`, then `"zoo"` should come before `"apple"` in that ordering, and your function should detect when a pair of adjacent words violates the order.

## Approach

1. **Map each character to its index** in the custom alphabet so you can compare characters in O(1).
2. **Walk through adjacent pairs** of words. For each pair, determine the first character position where they differ.
3. **Compare those characters** using your custom order. If the first word’s character has a *higher* index (comes later) than the second’s, the list is not sorted — return `false`.
4. **Handle prefixes:** If one word is a prefix of the other, the shorter one must come first (e.g. `"app"` before `"apple"`), otherwise return `false`.
5. If all pairs are valid, return `true`.

## Example solution in C#

```csharp
public static bool IsSortedCustom(string[] words, string order)
{
    var orderIndex = new Dictionary<char, int>();
    for (int i = 0; i < order.Length; i++)
        orderIndex[order[i]] = i;

    for (int i = 0; i < words.Length - 1; i++)
    {
        string a = words[i], b = words[i + 1];
        int minLen = Math.Min(a.Length, b.Length);

        for (int j = 0; j < minLen; j++)
        {
            if (a[j] != b[j])
            {
                if (orderIndex[a[j]] > orderIndex[b[j]])
                    return false;
                break;
            }
        }
        // If we didn't break and a is longer than b, a should not come first
        if (a.Length > b.Length)
            return false;
    }

    return true;
}
```

- **orderIndex** gives O(1) lookup for “which position is this letter in the custom order?”
- We only compare the first differing character; if it’s in order we move on, if not we return `false`.
- The final check ensures that when one word is a prefix of the other, the shorter one is first.

## Try it yourself

If you haven’t solved it yet, try implementing it from scratch or in another language. You can validate with examples like:

- Custom order `"abcdefghijklmnopqrstuvwxyz"` → behaves like normal dictionary order.
- Custom order `"zyxwvutsrqponmlkjihgfedcba"` → reversed order.

If you’ve got a different approach or a cleaner solution, I’d love to see it — drop a comment or share your version.

## Sitemap

See the full [sitemap](https://isiahjones.dev/sitemap.md) for all pages.
