1.txt,10.txt ,2.txt,3txt 排序
怎么c# 编写 把1.txt,10.txt ,2.txt,3txt 排序成为 1.txt,2.txt,3txt 10.txt
我是个初学者 请各位帮帮忙 C#
[解决办法]
public class NameSorter : IComparer
{
public int Compare(object x, object y)
{
if (x == null && y == null)
{
return 0;
}
if (x == null)
{
return -1;
}
if (y == null)
{
return 1;
}
FileInfo xInfo = new FileInfo(x.ToString());
FileInfo yInfo = new FileInfo(y.ToString());
if (xInfo.FullName.Length > yInfo.FullName.Length)
{
return 0;
}
return xInfo.FullName.CompareTo(yInfo.FullName);
}
}
string[] fileList = Directory.GetFiles(currentPath);
Array.Sort(fileList, NameSorter());
[解决办法]
有时间,参考这个:
http://www.cnblogs.com/insus/p/3270191.html
[解决办法]
static void Main(string[] args)
{
string str = "1.txt,10.txt,2.txt,3.txt";
IEnumerator enumerator = str.Split(',').OrderBy(x => Convert.ToInt32(Regex.Match(x, @"\d+").Value)).GetEnumerator();
while (enumerator.MoveNext())
{
Console.WriteLine((string)enumerator.Current);
}
}
有道理
[解决办法]
自己实现IComparer接口,象这个例子:http://stackoverflow.com/questions/9988937/sort-string-numbers
[解决办法]
或者参考这个,有现成的代码:http://www.interact-sw.co.uk/iangblog/2007/12/13/natural-sorting