10

为了在我的应用程序中进行自然排序,我目前在 shlwapi.dll 中 P/Invoke 一个名为 StrCmpLogicalW 的函数。我正在考虑尝试在 Mono 下运行我的应用程序,但是当然我不能拥有这个 P/Invoke 的东西(据我所知)。

是否有可能在某处看到该方法的实现,或者是否有一个好的、干净和高效的 C# 片段可以做同样的事情?

我的代码目前如下所示:

[SuppressUnmanagedCodeSecurity]
internal static class SafeNativeMethods
{
    [DllImport("shlwapi.dll", CharSet = CharSet.Unicode)]
    public static extern int StrCmpLogicalW(string psz1, string psz2);
}

public class NaturalStringComparer : IComparer<string>
{
    private readonly int modifier = 1;

    public NaturalStringComparer() : this(false) {}
    public NaturalStringComparer(bool descending)
    {
        if (descending) modifier = -1;
    }

    public int Compare(string a, string b)
    {
        return SafeNativeMethods.StrCmpLogicalW(a ?? "", b ?? "") * modifier;
    }
}

所以,我正在寻找的是不使用外部函数的上述类的替代方案。

4

4 回答 4

10

我刚刚在 C# 中实现了自然字符串比较,也许有人会觉得它很有用:

public class NaturalComparer : IComparer<string>
{
   public int Compare(string x, string y)
   {
      if (x == null && y == null) return 0;
      if (x == null) return -1;
      if (y == null) return 1;

      int lx = x.Length, ly = y.Length;

      for (int mx = 0, my = 0; mx < lx && my < ly; mx++, my++)
      {
         if (char.IsDigit(x[mx]) && char.IsDigit(y[my]))
         {
            long vx = 0, vy = 0;

            for (; mx < lx && char.IsDigit(x[mx]); mx++)
               vx = vx * 10 + x[mx] - '0';

            for (; my < ly && char.IsDigit(y[my]); my++)
               vy = vy * 10 + y[my] - '0';

            if (vx != vy)
               return vx > vy ? 1 : -1;
         }

         if (mx < lx && my < ly && x[mx] != y[my])
            return x[mx] > y[my] ? 1 : -1;
      }

      return lx - ly;
   }
}
于 2011-04-12T20:29:13.310 回答
5

http://www.interact-sw.co.uk/iangblog/2007/12/13/natural-sorting似乎是您正在寻找的。

(不,没有托管等效于 .NET 中内置的 StrCmpLogicalW)

于 2012-01-09T19:21:23.567 回答
0

我使用正则表达式来删除特殊字符。然后转换为 int。然后我比较了整数。

输入 :

列表输入 = 新列表{ "6.04","6.01","6.03","6#04" };
预期输出:

6.01
6.03
6.04
6#04
 var output = input.OrderBy(s => s, new NaturalStringComparer());
            foreach(输出中的var排序)
            {
                Console.WriteLine(排序);
            }


公共结构 NaturalStringComparer : IComparer
    {
        公共 int 比较(字符串 x,字符串 y)
        {
            if (x == null && y == null) 返回 0;
            如果(x == null)返回-1;
            如果(y == null)返回1;

            int lx = x.Length,ly = y.Length;

            int a = int.Parse(System.Text.RegularExpressions.Regex.Replace(x, @"\D+", ""));
            int b = int.Parse(System.Text.RegularExpressions.Regex.Replace(y, @"\D+", ""));

            返回 a.CompareTo(b);
        }
    }
于 2017-04-24T09:35:24.670 回答
0

如果您在 Windows XP 或更新版本上运行,您可以 PInvoke 到 shell 函数 StrCmpLogicalW:

public static int StrCmpLogical(String s1, String s2)
{
    if (String.IsNullOrEmpty(s1) && !String.IsNullOrEmpty(s2))
        return 1; //empty s1 comes after s2
    else if (String.IsNullOrEmpty(s2) && !String.IsNullOrEmpty(s1))
        return -1; //non-empty string comes before empty

    return SafeNativeMethods.StrCmpLogicalW(s1, s2);
}

然后是内部的不安全类:

/// <summary>
/// This class suppresses stack walks for unmanaged code permission. 
/// (System.Security.SuppressUnmanagedCodeSecurityAttribute is applied to this class.) 
/// This class is for methods that are safe for anyone to call. 
/// Callers of these methods are not required to perform a full security review to make sure that the 
/// usage is secure because the methods are harmless for any caller.
/// </summary>
[SuppressUnmanagedCodeSecurity]
internal static class SafeNativeMethods
{
    [DllImport("shlwapi.dll", CharSet = CharSet.Unicode)]
    internal static extern Int32 StrCmpLogicalW(string psz1, string psz2);
}
于 2019-02-21T17:07:58.793 回答