38 lines
1.3 KiB
C#
38 lines
1.3 KiB
C#
|
|
using System;
|
|||
|
|
using System.Collections.Generic;
|
|||
|
|
using System.Runtime.InteropServices;
|
|||
|
|
|
|||
|
|
namespace DTS.Common.Utilities
|
|||
|
|
{
|
|||
|
|
/// <summary>
|
|||
|
|
/// this is a class for doing natural string compares like in explorer (using the explorer function)
|
|||
|
|
/// http://msdn.microsoft.com/en-us/library/bb759947%28VS.85%29.aspx
|
|||
|
|
/// </summary>
|
|||
|
|
/// <remarks>requires XP or greater</remarks>
|
|||
|
|
public class NaturalStringComparer : IComparer<string>
|
|||
|
|
{
|
|||
|
|
[DllImport("shlwapi.dll", CharSet = CharSet.Unicode, ExactSpelling = true)]
|
|||
|
|
static extern int StrCmpLogicalW(String x, String y);
|
|||
|
|
|
|||
|
|
/// <summary>
|
|||
|
|
/// do a natural string compare between two strings
|
|||
|
|
/// </summary>
|
|||
|
|
/// <param name="x">left string to compare</param>
|
|||
|
|
/// <param name="y">right string to compare</param>
|
|||
|
|
/// <returns>-1 if natural order of x is less than y, +1 if x is after y, 0 if they are equal</returns>
|
|||
|
|
public int Compare(string x, string y)
|
|||
|
|
{
|
|||
|
|
if (null == x) { return -1; }
|
|||
|
|
if (null == y) { return 1; }
|
|||
|
|
return StrCmpLogicalW(x, y);
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
public static int StaticCompare(string x, string y)
|
|||
|
|
{
|
|||
|
|
if (null == x) { return -1; }
|
|||
|
|
if (null == y) { return 1; }
|
|||
|
|
return StrCmpLogicalW(x, y);
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
}
|