Custom Alphabetical Order — A C# Coding Challenge

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:
trueif the words are in non-descending order according to the custom alphabet; otherwisefalse.
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
- Map each character to its index in the custom alphabet so you can compare characters in O(1).
- Walk through adjacent pairs of words. For each pair, determine the first character position where they differ.
- 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. - Handle prefixes: If one word is a prefix of the other, the shorter one must come first (e.g.
"app"before"apple"), otherwise returnfalse. - If all pairs are valid, return
true.
Example solution in C#
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.
Thanks for reading!
If you found this helpful, consider sharing it with others.
Building an Ad-Free Tic-Tac-Toe PWA with Cursor—With My Nephew in the Room
Tired of ads ruining simple games with my 5-year-old nephew, I built a tic-tac-toe PWA with Cursor while he watched—no code literacy needed, just seeing something get made. We tested on his iPad and kept adding features as we played.
Frontend Mentor—Learn to Code by Building Real Projects
Frontend Mentor offers 100+ design-to-code challenges that mirror real-world work. Here's why it's become a go-to for developers building portfolio-worthy projects.