Windows资源管理器的排序方法资源管理器、方法、Windows

2023-09-11 00:15:06 作者:孤浪王者

我在寻找一种算法,排序的方式类似的文件(夹)的字符串进行排序在Windows资源管理器。看来,在串数值是考虑到当分拣导致像

  1名,2名,10名
 

而不是

  1名,10名,姓名2
 
Windows10系统从资源管理器打开常用工具的方法

,你得到一个普通字符串比较。

我正要开始写这个自己,但要检查是否有人之前就做到了这一点,并愿意分享一些code或见解。我会接近这个问题的方法是比较它们之前前导零添加到数值之名。这将导致像

  00001名,00010名,00002名
 

当与一个普通字符串来分类排序会给我正确的结果。

任何想法?

解决方案

这就是所谓的自然排序。杰夫有 pretty的大量博客文章就可以了前一阵子,这说明你可能会忽视的困难和链接到几个实现。

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?

解决方案

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.