我对C#几乎一无所知。话虽这么说,我的任务是在一个文件夹中根据最新版本的jQuery对列表进行排序。
目前,这就是我所拥有的:
public ActionResult Index()
{
//creating a DirectoryInfo object
DirectoryInfo mydir = new DirectoryInfo(@"\\edcapptest\E$\cdn\js\jquery");
// getting the files in the directory
FileInfo[] f = mydir.GetFiles();
List<string> myList = new List<string>();
foreach (FileInfo file in f)
{
myList.Add(file.Name);
}
myList.Sort();
return View(myList);
}
我一直在考虑如何在几天内完成这项工作,并且几乎没有结果(至少是有效的)。
非常感谢任何建议或帮助。
答案 0 :(得分:1)
假设按字母排序将按正确顺序排列...获取有序列表的代码可能如下所示....
我使用LINQ方法语法并评论每一行,因为你提到你对C#一无所知。
public ActionResult Index()
{
//creating a DirectoryInfo object
DirectoryInfo mydir = new DirectoryInfo(@"\\edcapptest\E$\cdn\js\jquery");
// getting the files in the directory
var myList = mydir.GetFiles() // Your objects in the list you start with
// Filter for the jquery files... you may not need this line
.Where(file => file.Name.StartsWith("jquery-"))
// Select the filename and version number so we can sort
.Select(file => new { Name= file.Name, Version = GetVersion(file.Name)})
// OrderBy the version number
.OrderBy(f => f.Version)
// We have to select just the filename since that's all you want
.Select(f => f.Name)
// Convert your output into a List
.ToList();
return View(myList);
}
// GetVersion code and regex to remove characters...
private static Regex digitsOnly = new Regex(@"[^\d]");
public static Version GetVersion(string filename)
{
var versionNumbers = new List<int>();
var splits = filename.Split('.');
foreach (var split in splits)
{
var digits = digitsOnly.Replace(filename, "");
if (!string.IsNullOrEmpty(digits))
versionNumbers.Add(int.Parse(digits));
}
// Create a version which can have various major / minor versions and
// handles sorting for you.
return new Version(versionNumbers[0], versionNumbers[1],
versionNumbers[2]);
}
答案 1 :(得分:0)
不打算成为一个完整的解决方案,但这应该让你接近。
public List<jquery> GetVersion(string path)
{
var thelist = (from afile in System.IO.Directory.EnumerateFiles(path)
let version = removeLetters(afile)
select removeLetters(afile).Split('.')
into splitup
select new jquery()
{
Filename = filename,
//will want to add some additional checking here as if the length is not 3 then there will be other errors.
//just a starting point for you
Major = string.IsNullOrWhiteSpace(splitup[0]) ? 0 : int.Parse(splitup[0]),
Minor = string.IsNullOrWhiteSpace(splitup[1]) ? 0 : int.Parse(splitup[1]),
Build = string.IsNullOrWhiteSpace(splitup[2]) ? 0 : int.Parse(splitup[2]),
}).ToList();
return thelist
.OrderByDescending(f => f.Major)
.ThenByDescending(f => f.Minor)
.ThenByDescending(f => f.Build)
.ToList();
}
public string removeLetters(string value)
{
var chars = value.ToCharArray().Where(c => Char.IsNumber(c) || c.Equals('.'));
return chars.ToString();
}
public class jquery
{
public string Filename { get; set; }
public int Major { get; set; }
public int Minor { get; set; }
public int Build { get; set; }
}
答案 2 :(得分:0)
我已经创建了一个类来为我完成所有工作,但是你不必这样做,我发现它更容易。我使用正则表达式从文件名中提取版本号,然后将其拆分为版本号的3个整数部分。
public class JQueryVersion
{
public string File_Name { get; set; }
public string Version
{
get
{
return Regex.Match(this.File_Name, @"jquery-([0-9]{1,2}\.[0-9]{1,2}\.[0-9]{1,2})\.").Groups[1].Value;
}
}
public int[] Version_Numeric
{
get
{
return this.Version.Split('.').Select(v => int.Parse(v)).ToArray();
}
}
public JQueryVersion (string File_Name)
{
this.File_Name = File_Name;
}
}
然后,您可以创建JQueryVersion
对象列表并对其进行排序:
List<JQueryVersion> Data = new List<JQueryVersion>()
{
new JQueryVersion("jquery-1.10.2.min.js"),
new JQueryVersion("jquery-1.11.0.min.js"),
new JQueryVersion("jquery-1.5.1.min.js"),
new JQueryVersion("jquery-1.6.1.min.js"),
new JQueryVersion("jquery-1.6.2.min.js"),
};
var Sorted_Data = Data
.OrderByDescending (d => d.Version_Numeric[0])
.ThenByDescending (d => d.Version_Numeric[1])
.ThenByDescending (d => d.Version_Numeric[2]);
答案 3 :(得分:0)
我个人使用Sort(比较&lt; T&gt;)进行自制比较:
http://msdn.microsoft.com/en-us/library/w56d4y5z%28v=vs.110%29.aspx
以下是比较文件修改日期的示例:
public ActionResult Index()
{
//creating a DirectoryInfo object
DirectoryInfo mydir = new DirectoryInfo(@"\\edcapptest\E$\cdn\js\jquery");
// getting the files in the directory
FileInfo[] f = mydir.GetFiles();
List<string> myList = new List<string>();
foreach (FileInfo file in f)
{
myList.Add(file.Name);
}
myList.Sort(SortByModificationDate);
return View(myList);
}
public int SortByModificationDate(string file1, string file2)
{
DateTime time1 = File.GetLastWriteTime(file1);
DateTime time2 = File.GetLastWriteTime(file2);
return time1.CompareTo(time2);
}