"If a worker wants to do his job well, he must first sharpen his tools." - Confucius, "The Analects of Confucius. Lu Linggong"
Front page > Programming > Interview experts teach you simple file sorting skills

Interview experts teach you simple file sorting skills

Posted on 2025-04-21
Browse:208

A Lesson in Simplicity: Sorting Files Like a Pro in an Interview

During a technical interview for a full-stack developer role, I found myself in a familiar yet unexpectedly challenging situation. Everything had been going smoothly until the interviewer presented me with a task that seemed simple at first.

“Can you sort these filenames the way they’d appear in a file explorer, in ascending order?” they asked.

I thought, “Piece of cake.” Sorting is such a fundamental operation that I didn't expect any trouble. But as soon as I started writing the code, I hit a snag. The filenames were all over the place some were simple, but others included numbers, letters, and combinations of both.

I tried using a basic string sorting method like:
array.sort();
But this produced an odd result. The numbers were sorted lexicographically (meaning “10” would come before “2” because it starts with a “1”), and mixed alphanumeric strings weren’t in the correct order. It was a mess, and it didn’t resemble the natural order you see in file explorers at all.

I could feel the clock ticking and pressure building up. I tried to work through various custom comparison functions to handle the numbers properly, but nothing seemed to click.

Then, I remembered a simple trick I had read about not too long ago: localeCompare.

localeCompare allows you to compare strings in a way that mimics how humans sort things. By using it with the numeric option, it handles the numbers as actual numbers rather than comparing them as text. Here’s the code I used:
array.sort((a, b) => a.localeCompare(b, undefined, { numeric: true, sensitivity: 'base' }));
This single line of code sorted the filenames exactly like a file explorer would: numbers were ordered naturally, and letter/number combinations were handled seamlessly.

As soon as I ran the code, I saw the correct order appear on the screen. The filenames were perfectly sorted in ascending order, just like in a file explorer. I smiled as I explained the solution to the interviewer, who seemed pleased with how I resolved the issue.

What initially felt like a complicated problem ended up being solved with a simple, elegant solution. It was a reminder that sometimes the most effective tools are the simplest, and knowing those small tricks can make all the difference.

Release Statement This article is reproduced at: https://dev.to/janvinsha/a-lesson-in-simplicity-sorting-files-like-a-pro-in-an-interview-1nlo?1 If there is any infringement, please contact [email protected] to delete it.
Latest tutorial More>

Disclaimer: All resources provided are partly from the Internet. If there is any infringement of your copyright or other rights and interests, please explain the detailed reasons and provide proof of copyright or rights and interests and then send it to the email: [email protected] We will handle it for you as soon as possible.

Copyright© 2022 湘ICP备2022001581号-3