110,571
社区成员
发帖
与我相关
我的任务
分享
DirectoryInfo dir = new DirectoryInfo(oldPath);
int i=0;
foreach (FileInfo f in dir.GetFiles("*.txt"))
{
String name = f.Name;
nameList.Add(i,name);//nameList是一个哈希表
i++;
}
private class ComparerFile : IComparer<string>
{
public int Compare(string x, string y)
{
string strX = null;
string strY = null;
double dblX = 0;
double dblY = 0;
int intPos = 0;
strX=System.IO.Path.GetFileNameWithoutExtension(x);
strY=System.IO.Path.GetFileNameWithoutExtension(y);
dblX = Conversion.Val(strX);
dblY = Conversion.Val(strY);
if (dblX == dblY)
{
return x.CompareTo(y);
}
else
{
return dblX.CompareTo(dblY);
}
}
}
private class ComparerFile : IComparer<string>
{
public int Compare(string x, string y)
{
string strX = null;
string strY = null;
double dblX = 0;
double dblY = 0;
int intPos = 0;
strX=System.IO.Path.GetFileNameWithoutExtension(x);
strY=System.IO.Path.GetFileNameWithoutExtension(y);
dblX = Conversion.Val(strX);
dblY = Conversion.Val(strY);
intPos = strX.IndexOf(dblX.ToString());
strX = strX.Substring(intPos + 1);
intPos = strY.IndexOf(dblY.ToString());
strY = strY.Substring(intPos + 1);
if (dblX == dblY)
{
return strX.CompareTo(strY);
}
else
{
return dblX.CompareTo(dblY);
}
}
}
string[] strFiles = null;
ComparerFile objComparer = new ComparerFile();
strFiles = System.IO.Directory.GetFiles(oldPath, "*.TXT");
Array.Sort(strFiles, objComparer);
using System;
using System.Collections.Generic;
using System.Text;
using System.Text.RegularExpressions;
using System.Collections;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
string[] str = new string[10];
str[0] = "7abcd";
str[1] = "4efgh";
str[2] = "3desw";
str[3] = "2ferf";
str[4] = "18uhyt";
str[5] = "5aiuy";
str[6] = "22bbby";
str[7] = "10rted";
str[8] = "9artd";
str[9] = "17uyij";
Console.WriteLine("排序前");
for (int i = 0; i < str.Length; i++)
{
Console.WriteLine(str[i]);
}
Console.WriteLine("\n排序之后");
string[] str2 = getSortString(str);
for (int i = 0; i < str2.Length; i++)
{
Console.WriteLine(str2[i]);
}
Console.Read();
}
/// <summary>
/// 将传入数组中以数字开头的元素保留下来并按照数字排序
/// 开头的数字不可以有重复值,否则出现异常
/// </summary>
/// <param name="str"></param>
/// <returns>排序号的数组</returns>
private static string[] getSortString(string[] str)
{
int[] orderStr = new int[str.Length];
Hashtable list = new Hashtable();
string[] overString=new string [str.Length];
for (int i = 0; i < str.Length; i++)
{
string pattern = @"\b[0-9]{1,5}";
MatchCollection Matches = Regex.Matches(str[i], pattern, RegexOptions.IgnoreCase | RegexOptions.ExplicitCapture);
foreach (Match var in Matches)
{
orderStr[i] = int.Parse(var.Value);
list.Add(var.Value, str[i]);
}
}
Array.Sort(orderStr);
for (int i = 0; i < orderStr.Length; i++)
{
if (orderStr[i] != 0)
{
overString[i]=list[orderStr[i].ToString()].ToString();
}
}
return overString;
}
}
}
static void Main(string[] args)
{
List<string> list = new List<string>();
list.Add("2-hh.txt");
list.Add("20-hh.txt");
list.Add("12-hh.txt");
list.Add("1-hh.txt");
list.Sort();
foreach (string var in list)
{
Console.WriteLine(var);
}
Console.Read();
}
List<int> list = new List<int>();
list.Add(2);
list.Add(22);
list.Add(1);
list.Add(12);
list.Sort();
foreach (int var in list)
{
Console.WriteLine(var);
}