排序列表< FileInfo>按自然排序顺序. [英] Sorting List<FileInfo> in Natural sorted order .

查看:319
本文介绍了排序列表< FileInfo>按自然排序顺序.的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个列表,该列表是从WCF服务返回的,用于服务器上的文件列表. 以后我用来在客户端应用程序中填充TreeView的

I have a List which is being returned from a WCF service ,for List of files on Server. Which i am later using to populate the TreeView in my Client Application

我需要列表按自然排序.

I need the list to be in naturally sorted order.

例如: 我有一个类似List-1的列表,我的预期结果是List-2 但我得到List-3作为输出.(请参阅下面的列表)

For ex : I have a list something like List-1 , and my expected result is List-2 but i get List-3 as my output.(refer list given below)

List-1          List-2          List-3
abc.jpg         abc.jpg         abc.jpg
abc10.jpg       abc10.jpg       abc10.jpg
abc100.jpg      abc97.jpg       abc100.jpg
abc98.jpg       abc98.jpg       abc101.jpg
abc97.jpg       abc100.jpg      abc102.jpg
abc102.jpg      abc101.jpg      abc97.jpg
abc101.jpg      abc102.jpg      abc98.jpg

到目前为止,我已经研究了stackoverflow的帖子:

So far i have looked into the stackoverflow's post :

[1]: C#排序文件通过名称中的自然数排序? [2]:

[1] :C# Sort files by natural number ordering in the name? [2]: Sorting a FileInfo[] based using Natural Sorting on the filename (SQL files) .

他们都没有找到适合我的情况的工作,任何帮助将不胜感激:)

None of them found to working for my case , Any help would be appreciated :)

推荐答案

用于自然排序的便捷列表扩展:

Here you go; a handy list extension for natural sorting:

using System;
using System.Collections.Generic;
using System.Runtime.InteropServices;

namespace Demo
{
    // A List extension class for natural sorting.

    public static class ListExt
    {
        [DllImport("shlwapi.dll", CharSet = CharSet.Unicode)]
        private static extern int StrCmpLogicalW(string lhs, string rhs);

        // Version for lists of any type.
        public static void SortNatural<T>(this List<T> self, Func<T, string> stringSelector)
        {
            self.Sort((lhs, rhs) => StrCmpLogicalW(stringSelector(lhs), stringSelector(rhs)));
        }

        // Simpler version for List<string>
        public static void SortNatural(this List<string> self)
        {
            self.Sort(StrCmpLogicalW);
        }
    }

    // Demonstrate using the List extension.

    public class Program
    {
        private static void Main(string[] args)
        {
            var names = new List<FileInfo>
            {
                new FileInfo("abc.jpg"),
                new FileInfo("abc10.jpg"),
                new FileInfo("abc100.jpg"),
                new FileInfo("abc98.jpg"),
                new FileInfo("abc97.jpg"),
                new FileInfo("abc102.jpg"),
                new FileInfo("abc101.jpg")
            };

            names.SortNatural(x => x.Name);

            foreach (var name in names)
                Console.WriteLine(name);
        }
    }
}

该程序的输出为:

abc.jpg
abc10.jpg
abc97.jpg
abc98.jpg
abc100.jpg
abc101.jpg
abc102.jpg

这利用了Windows API

This takes advantage of the Windows API StrCmpLogicalW() method which does a natural sort order comparison, and uses P/Invoke to call it.

这篇关于排序列表&lt; FileInfo&gt;按自然排序顺序.的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆