Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Windows Explorer sort method

Tags:

I'm looking for an algorithm that sorts strings similar to the way files (and folders) are sorted in Windows Explorer. It seems that numeric values in strings are taken into account when sorted which results in something like

name 1, name 2, name 10 

instead of

name 1, name 10, name 2 

which you get with a regular string comparison.

I was about to start writing this myself but wanted to check if anyone had done this before and was willing to share some code or insights. The way I would approach this would be to add leading zeros to the numeric values in the name before comparing them. This would result in something like

name 00001, name 00010, name 00002 

which when sorted with a regular string sort would give me the correct result.

Any ideas?

like image 459
Christophe Herreman Avatar asked Jan 14 '09 09:01

Christophe Herreman


People also ask

What sorting algorithm does Windows use?

It uses the QuickSort algorithm.

How do I customize sort in File Explorer?

If Categories column isn't showing, right-click anywhere within the File Explorer, select Sort by and select More from the list. Put a check mark in the Categories option under Details. Once you're done, you can already set it up and sort the folder by Categories.

How do I sort in Windows Explorer?

One option to begin sorting files and folders is to right-click or press-and-hold on a free area inside the folder to open a contextual menu. Then, hover or tap on Sort by to reveal the four main sorting options for that view template.


1 Answers

It's called "natural sort order". Jeff had a pretty extensive blog entry on it a while ago, which describes the difficulties you might overlook and has links to several implementations.

like image 128
Michael Borgwardt Avatar answered Sep 23 '22 21:09

Michael Borgwardt