文件名智能排序(按照数字大小排序)
2014-03-13 12:37
232 查看
有时候需要将文件名按照数字大小排序,而不是按照字母顺序排序,例如:
在Windows XP及以后的系统中,通过修改注册表,或者使用组策略(实质上还是修改注册表),可以控制使用哪种排序方式,进行文件名排序。在
HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Policies\Explorer下面创建
DWORD键:NoStrCmpLogical,然后设置值为1,禁止使用智能排序;设置为0,启用智能排序。
程序实现智能排序,有人已经写了,在这转一下C#实现。最后的参考列表中的网址上,有各种语言的实现方法。
参考:
http://www.davekoelle.com/alphanum.html
http://www.askvg.com/how-to-disable-numerical-file-name-sorting-and-enable-classic-literal-sorting-in-windows-xp-vista-and-7-explorer/
本文出自 “GONE WITH THE WIND” 博客,请务必保留此出处http://h2appy.blog.51cto.com/609721/1375457
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 |
HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Policies\Explorer下面创建
DWORD键:NoStrCmpLogical,然后设置值为1,禁止使用智能排序;设置为0,启用智能排序。
程序实现智能排序,有人已经写了,在这转一下C#实现。最后的参考列表中的网址上,有各种语言的实现方法。
/* * 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; } } }
参考:
http://www.davekoelle.com/alphanum.html
http://www.askvg.com/how-to-disable-numerical-file-name-sorting-and-enable-classic-literal-sorting-in-windows-xp-vista-and-7-explorer/
本文出自 “GONE WITH THE WIND” 博客,请务必保留此出处http://h2appy.blog.51cto.com/609721/1375457
相关文章推荐
- 按照文件名的数字大小排序文件
- mysql将数字组成的字符串字段按照字符串数值型的大小进行排序
- excel表排序如何按照数字大小排序?
- 批量按照文件大小排序并修改文件名
- mysql将数字组成的字符串字段按照字符串数值型的大小进行排序
- 如何让SQLServer的id按照数字大小顺序排序
- mysql将数字组成的字符串字段按照字符串数值型的大小进行排序
- HashSet排序【自定义一种排序方式:例如按照字符串的长短比较大小。如长度相同,就以数字比较】
- js,将一个整数数组先按照因子数量排序,再按照数字大小排序
- 如何让SQLServer的 itemNum 字段 按照数字大小顺序排序
- shell 按照文件中指定列的数字大小排序
- MySQL取出数字字符串的一部分,然后按照数值大小排序
- 一组文件名(主要以数字命名), 我想按数字大小排序
- js,将一个整数数组先按照因子数量排序,再按照数字大小排序 Version2
- 一个一维整形数组,若干个数字,统计数组中不同的数字出现的次数,并按照出现频率从小到大排序输出,相同频率按数字大小排序输出
- DIR 按文件名中数字大小进行排序
- mysql 字符串里面的数字,按照大小排序
- 可以按照数字大小排序的列表类
- 如何让SQLServer的 itemNum 字段 按照数字大小顺序排序