文件夹下文件名正确排序问题
来源:互联网 发布:富人国 知乎 编辑:程序博客网 时间:2024/06/04 18:12
有时候需要将文件名按照数字大小排序,而不是按照字母顺序排序,例如:
Alphanum
1000X Radonius Maximus10X Radonius200X Radonius20X Radonius20X Radonius Prime30X Radonius40X RadoniusAllegia 50 ClasteronAllegia 500 ClasteronAllegia 50B ClasteronAllegia 51 ClasteronAllegia 6R ClasteronAlpha 100Alpha 2Alpha 200Alpha 2AAlpha 2A-8000Alpha 2A-900Callisto MorphamaxCallisto Morphamax 500Callisto Morphamax 5000Callisto Morphamax 600Callisto Morphamax 6000 SECallisto Morphamax 6000 SE2Callisto Morphamax 700Callisto Morphamax 7000Xiph Xlater 10000Xiph Xlater 2000Xiph Xlater 300Xiph Xlater 40Xiph Xlater 5Xiph Xlater 50Xiph Xlater 500Xiph Xlater 5000Xiph Xlater 58
10X Radonius20X Radonius20X Radonius Prime30X Radonius40X Radonius200X Radonius1000X Radonius MaximusAllegia 6R ClasteronAllegia 50 ClasteronAllegia 50B ClasteronAllegia 51 ClasteronAllegia 500 ClasteronAlpha 2Alpha 2AAlpha 2A-900Alpha 2A-8000Alpha 100Alpha 200Callisto MorphamaxCallisto Morphamax 500Callisto Morphamax 600Callisto Morphamax 700Callisto Morphamax 5000Callisto Morphamax 6000 SECallisto Morphamax 6000 SE2Callisto Morphamax 7000Xiph Xlater 5Xiph Xlater 40Xiph Xlater 50Xiph Xlater 58Xiph Xlater 300Xiph Xlater 500Xiph Xlater 2000Xiph Xlater 5000Xiph Xlater 10000使用:
private void button1_Click_1(object sender, EventArgs e) { this.treeView1.Nodes.Clear(); if( this.folderBrowserDialog1.ShowDialog()==DialogResult.OK ) { this.label6.Text =this.folderBrowserDialog1.SelectedPath; workpath = this.folderBrowserDialog1.SelectedPath; DirectoryInfo dir = new DirectoryInfo(workpath); FileInfo[] arrFi = dir.GetFiles("*.*"); //FileInfo[] files = dir.GetFiles(); SortAsFileName(ref arrFi); for (int i = 0; i < arrFi.Length; i++) { this.treeView1.Nodes.Add(arrFi[i].Name); } } } private void SortAsFileName(ref FileInfo[] arrFi) { AlphanumComparator alph = new AlphanumComparator(); //Array.Sort(arrFi, delegate(FileInfo x, FileInfo y) { return x.Name.CompareTo(y.Name); }); Array.Sort(arrFi, delegate(FileInfo x, FileInfo y) { return alph.Compare(x.Name, y.Name); }); }智能排序类:
/* * 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
阅读全文
0 0
- 文件夹下文件名正确排序问题
- 读取本地文件夹下的图片文件名,进行比较排序
- 提取文件夹下文件名
- 获取文件夹下所有文件名
- 搜索本地文件夹下文件名
- matlab 文件夹下文件名修改
- 修改文件夹下,文件名编码
- 文件夹下的所有文件名
- Ubuntu下文件夹内文件排序问题
- 解决tomcat下不能访问中文文件夹下或中文文件名问题
- glib下的文件名排序
- 获取某一文件夹下文件名数组
- 如何读取文件夹下的所有文件名
- 获得文件夹下文件名列表的方法
- 获取文件夹下的所有文件名
- python3 批量修改当前文件夹下 文件名
- C++ 获取文件夹下的所有文件名
- c++读取文件夹下的所有文件名
- CentOS系统下的Hadoop集群(第2期)_机器信息分布表
- Reader-Aware Multi-Document Summarization: An Enhanced Model and The First Dataset
- kubernetes1.8原生dashboard部署
- 专为物联网而生的操作系统——TinyOS
- Leetcode 120.Triangle
- 文件夹下文件名正确排序问题
- Ubuntu 16.04 mount error
- 模拟游戏引擎中的灯光实现
- 消息队列系列之分布式消息队列Kafka
- 自定义----Banner 轮播加小圆点>> 获取网络图片 点击事件
- mysql-proxy安装配置
- Vim清空整个文件的小技巧
- Amazon Rekognition
- [Hexo] 溜了溜了
原创粉丝点击
热门IT博客
热门问题
老师的惩罚
人脸识别
我在镇武司摸鱼那些年
重生之率土为王
我在大康的咸鱼生活
盘龙之生命进化
天生仙种
凡人之先天五行
春回大明朝
姑娘不必设防,我是瞎子
一侧巧克力囊肿怎么办
怀孕8周胎停育怎么办
8周胎停育了怎么办
胎停育没出血怎么办
坐月子腰酸背疼怎么办
坐月子腰酸怎么办啊
坐月子腰酸怎么办
坐月子期间腰酸怎么办
胃部赘肉怎么办
胃部有息肉怎么办
胃肠绞痛拉肚子怎么办
胃部恶心不适怎么办
练瑜伽扭到腰怎么办
手脚很干怎么办
手脚干燥怎么办
脚太干脱皮怎么办
肩颈突出怎么办?
肩颈有一节突出怎么办?
思想邪恶怎么办
分手威胁恐吓怎么办
瑜伽垫太臭怎么办
佛教 风水不好怎么办
跪坐不下去怎么办
挖机大臂动作慢怎么办
运动完酸疼怎么办
上课被罚练坐无聊怎么办
排泄慢怎么办
消化慢怎么办
肠蠕动慢怎么办?
癌症晚期便秘怎么办
孕妇消化不好怎么办
胃疼发烧怎么办
崔玉涛小儿便秘怎么办
胃老是不舒服怎么办
少年肠胃不好怎么办
青少年胃肠不好怎么办
吸收功能差怎么办
消化系统功能紊乱怎么办
肠蠕动慢怎么办
小肠蠕动过快怎么办
胃幽门堵塞怎么办