猿问

在 List<Tuple<>>() 或 asp:ListView 中对由字符和数字组成的值进行排序

我有一个元组列表,我正在用一个文件夹的所有目录填充这个列表,并使用以下代码将它绑定到 asp:ListView:


List<string> directoryContent = new List<string>(Directory.GetFileSystemEntries(dirPath);

List<Tuple<string, string>> directoryList = new List<Tuple<string, string>>();


for (int i = 0; i < directoryContent.Count; i++)

{

    FileAttributes attr = File.GetAttributes(directoryContent[i]);

    if (attr.ToString().Equals("Directory"))

    {

        String str = directoryContent[i].Remove(0, dirPath.Length);

        string count = Directory.GetFiles(directoryContent[i], "*.*", SearchOption.AllDirectories).Length.ToString();

        directoryList.Add(new Tuple<string, string>(str, count));

    }

}


directoryListView.DataSource = directoryList;

directoryListView.DataBind();

例如找到的目录是


12

566

10001

10

模板

文件

以这种方式对 List 或 ListView 进行排序的最佳方法是什么?先感谢您。我需要对它们按以下顺序排序的目录进行排序:


文件

模板

10

12

566

1001


白衣非少年
浏览 496回答 1
1回答

临摹微笑

您可以为此使用 Linq。//test dataList<string> list = new List<string>(){&nbsp; &nbsp; "12",&nbsp; &nbsp; "566",&nbsp; &nbsp; "10001",&nbsp; &nbsp; "10",&nbsp; &nbsp; "templates",&nbsp; &nbsp; "files"};int tempInt;//filter the numbers from the list and sortvar listNumbers = list.Where(x => int.TryParse(x, out tempInt)).Select(y => Convert.ToInt32(y)).OrderBy(z => z);//filter the strings from the list and sortvar listStrings = list.Where(x => !int.TryParse(x, out tempInt)).OrderBy(y => y);//join the two lists againvar orderedList = listStrings.Concat(listNumbers.Select(y => y.ToString())).ToList();更新元组列表List<Tuple<string, string>> list = new List<Tuple<string, string>>(){&nbsp; &nbsp; new Tuple<string, string>("12", "NA"),&nbsp; &nbsp; new Tuple<string, string>("566", "NA"),&nbsp; &nbsp; new Tuple<string, string>("10001", "NA"),&nbsp; &nbsp; new Tuple<string, string>("10", "NA"),&nbsp; &nbsp; new Tuple<string, string>("templates", "NA"),&nbsp; &nbsp; new Tuple<string, string>("files", "NA")};int tempInt;//filter the numbers from the list and sortvar listNumbers = list.Where(x => int.TryParse(x.Item1, out tempInt)).Select(y => new Tuple<int, string>(Convert.ToInt32(y.Item1), y.Item2)).OrderBy(z => z.Item1);//filter the strings from the list and sortvar listStrings = list.Where(x => !int.TryParse(x.Item1, out tempInt)).OrderBy(z => z.Item1);//join the two lists againvar orderedList = listStrings.Concat(listNumbers.Select(y => new Tuple<string, string>(y.Item1.ToString(), y.Item2))).ToList();
随时随地看视频慕课网APP
我要回答