文件名智能排序(按照数字大小排序)

简介:

有时候需要将文件名按照数字大小排序,而不是按照字母顺序排序,例如:


Traditional Sort
Alphanum
1000X Radonius Maximus
10X Radonius
200X Radonius
20X Radonius
20X Radonius Prime
30X Radonius
40X Radonius
Allegia 50 Clasteron
Allegia 500 Clasteron
Allegia 50B Clasteron
Allegia 51 Clasteron
Allegia 6R Clasteron
Alpha 100
Alpha 2
Alpha 200
Alpha 2A
Alpha 2A-8000
Alpha 2A-900
Callisto Morphamax
Callisto Morphamax 500
Callisto Morphamax 5000
Callisto Morphamax 600
Callisto Morphamax 6000 SE
Callisto Morphamax 6000 SE2
Callisto Morphamax 700
Callisto Morphamax 7000
Xiph Xlater 10000
Xiph Xlater 2000
Xiph Xlater 300
Xiph Xlater 40
Xiph Xlater 5
Xiph Xlater 50
Xiph Xlater 500
Xiph Xlater 5000
Xiph Xlater 58
        

10X Radonius
20X Radonius
20X Radonius Prime
30X Radonius
40X Radonius
200X Radonius
1000X Radonius Maximus
Allegia 6R Clasteron
Allegia 50 Clasteron
Allegia 50B Clasteron
Allegia 51 Clasteron
Allegia 500 Clasteron
Alpha 2
Alpha 2A
Alpha 2A-900
Alpha 2A-8000
Alpha 100
Alpha 200
Callisto Morphamax
Callisto Morphamax 500
Callisto Morphamax 600
Callisto Morphamax 700
Callisto Morphamax 5000
Callisto Morphamax 6000 SE
Callisto Morphamax 6000 SE2
Callisto Morphamax 7000
Xiph Xlater 5
Xiph Xlater 40
Xiph Xlater 50
Xiph Xlater 58
Xiph Xlater 300
Xiph Xlater 500
Xiph Xlater 2000
Xiph Xlater 5000
Xiph Xlater 10000
        

   

   在Windows XP及以后的系统中,通过修改注册表,或者使用组策略(实质上还是修改注册表),可以控制使用哪种排序方式,进行文件名排序。在

HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Policies\Explorer下面创建

DWORD键:NoStrCmpLogical,然后设置值为1,禁止使用智能排序;设置为0,启用智能排序。


程序实现智能排序,有人已经写了,在这转一下C#实现。最后的参考列表中的网址上,有各种语言的实现方法。



1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
/*
  * The Alphanum Algorithm is an improved sorting algorithm for strings
  * containing numbers.  Instead of sorting numbers in ASCII order like
  * a standard sort, this algorithm sorts numbers in numeric order.
  *
  * The Alphanum Algorithm is discussed at http://www.DaveKoelle.com
  *
  * Based on the Java implementation of Dave Koelle's Alphanum algorithm.
  * Contributed by Jonathan Ruckwood <jonathan.ruckwood@gmail.com>
  *
  * Adapted by Dominik Hurnaus <dominik.hurnaus@gmail.com> to
  *   - correctly sort words where one word starts with another word
  *   - have slightly better performance
  *
  * This library is free software; you can redistribute it and/or
  * modify it under the terms of the GNU Lesser General Public
  * License as published by the Free Software Foundation; either
  * version 2.1 of the License, or any later version.
  *
  * This library is distributed in the hope that it will be useful,
  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  * Lesser General Public License for more details.
  *
  * You should have received a copy of the GNU Lesser General Public
  * License along with this library; if not, write to the Free Software
  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
  *
  */
using  System;
using  System.Collections;
using  System.Text;
/*
  * Please compare against the latest Java version at http://www.DaveKoelle.com
  * to see the most recent modifications
  */
namespace  AlphanumComparator
{
     public  class  AlphanumComparator : IComparer
     {
         private  enum  ChunkType {Alphanumeric, Numeric};
         private  bool  InChunk( char  ch,  char  otherCh)
         {
             ChunkType type = ChunkType.Alphanumeric;
             if  ( char .IsDigit(otherCh))
             {
                 type = ChunkType.Numeric;
             }
             if  ((type == ChunkType.Alphanumeric &&  char .IsDigit(ch))
                 || (type == ChunkType.Numeric && ! char .IsDigit(ch)))
             {
                 return  false ;
             }
             return  true ;
         }
         public  int  Compare( object  x,  object  y)
         {
             String s1 = x  as  string ;
             String s2 = y  as  string ;
             if  (s1 ==  null  || s2 ==  null )
             {
                 return  0;
             }
             int  thisMarker = 0, thisNumericChunk = 0;
             int  thatMarker = 0, thatNumericChunk = 0;
             while  ((thisMarker < s1.Length) || (thatMarker < s2.Length))
             {
                 if  (thisMarker >= s1.Length)
                 {
                     return  -1;
                 }
                 else  if  (thatMarker >= s2.Length)
                 {
                     return  1;
                 }
                 char  thisCh = s1[thisMarker];
                 char  thatCh = s2[thatMarker];
                 StringBuilder thisChunk =  new  StringBuilder();
                 StringBuilder thatChunk =  new  StringBuilder();
                 while  ((thisMarker < s1.Length) && (thisChunk.Length==0 ||InChunk(thisCh, thisChunk[0])))
                 {
                     thisChunk.Append(thisCh);
                     thisMarker++;
                     if  (thisMarker < s1.Length)
                     {
                         thisCh = s1[thisMarker];
                     }
                 }
                 while  ((thatMarker < s2.Length) && (thatChunk.Length==0 ||InChunk(thatCh, thatChunk[0])))
                 {
                     thatChunk.Append(thatCh);
                     thatMarker++;
                     if  (thatMarker < s2.Length)
                     {
                         thatCh = s2[thatMarker];
                     }
                 }
                 int  result = 0;
                 // If both chunks contain numeric characters, sort them numerically
                 if  ( char .IsDigit(thisChunk[0]) &&  char .IsDigit(thatChunk[0]))
                 {
                     thisNumericChunk = Convert.ToInt32(thisChunk.ToString());
                     thatNumericChunk = Convert.ToInt32(thatChunk.ToString());
                     if  (thisNumericChunk < thatNumericChunk)
                     {
                         result = -1;
                     }
                     if  (thisNumericChunk > thatNumericChunk)
                     {
                         result = 1;
                     }
                 }
                 else
                 {
                     result = thisChunk.ToString().CompareTo(thatChunk.ToString());
                 }
                 if  (result != 0)
                 {
                     return  result;
                 }
             }
             return  0;
         }
     }
}









本文转自 h2appy  51CTO博客,原文链接:http://blog.51cto.com/h2appy/1375457,如需转载请自行联系原作者
目录
相关文章
|
1月前
字符串,每个里面包含0-N个数字,如3,8,2,编写函数,将两个这样的字符串合并,并且输出的字符串里面没有重复的数字,并从大到小排列.
字符串,每个里面包含0-N个数字,如3,8,2,编写函数,将两个这样的字符串合并,并且输出的字符串里面没有重复的数字,并从大到小排列.
21 0
|
8月前
从排列字符串到排列序列:解析增减字符串匹配问题
题目要求根据给定的字符串 s,构造一个排列序列 perm,其中排列序列中的数字满足以下规则: 如果 perm[i] < perm[i + 1],则对应的字符为 'I'; 如果 perm[i] > perm[i + 1],则对应的字符为 'D'。 我们需要根据字符串 s 中的字符,构造满足上述规则的排列序列 perm。
40 0
|
9月前
|
C语言
数字的排序
原理:(升序)将一串乱序数组中的数字元素进行相邻两两比较,如果第一个数字大于第二个数字,进行交换。然后继续进行第二个和第三个比较,如果第二个数比第三个数大,进行交换,反之则继续下一次比较。数组进行第一次全数组比较将得到数组中最大的数字放在数组最后。
34 0
数字的排序
|
算法 Java
在排序数组中查找数字I(剑指offer 53-I)
在排序数组中查找数字I(剑指offer 53-I)
对一个list取前M个数字和后M个数字,形成两个列表
对一个list取前M个数字和后M个数字,形成两个列表
59 0
|
机器学习/深度学习 Java
Java数字黑洞给定任一个各位数字不完全相同的4位正整数,如果我们先把4个数字按非递增排序,再按非递减排序,然后用第1个数字减第2个数字,将得到 一个新的数字。一直重复这样做,我们很快会停在有“数字
Java数字黑洞给定任一个各位数字不完全相同的4位正整数,如果我们先把4个数字按非递增排序,再按非递减排序,然后用第1个数字减第2个数字,将得到 一个新的数字。一直重复这样做,我们很快会停在有“数字
96 0
|
存储
LeetCode 79单词搜索&80删除排序数组中的重复项Ⅱ&81.搜索旋转排序数组Ⅱ
单词必须按照字母顺序,通过相邻的单元格内的字母构成,其中“相邻”单元格是那些水平相邻或垂直相邻的单元格。同一个单元格内的字母不允许被重复使用。
57 0
LeetCode 79单词搜索&80删除排序数组中的重复项Ⅱ&81.搜索旋转排序数组Ⅱ
|
C++
201503-2 数字排序
201503-2 数字排序
43 0
201503-2 数字排序
|
Java
JAVA实现数字倒序输出
JAVA实现数字倒序输出
162 0
JAVA实现数字倒序输出