c# IComparer比较字符串

来源:互联网 发布:淘宝服务市场退款 编辑:程序博客网 时间:2024/05/22 06:21

例如:幻灯片50,幻灯片6,幻灯片40
排序完后:幻灯片6,幻灯片40,幻灯片50

public class PathCompare : IComparer<string>{    public int Compare(string x, string y)    {        string[] aBuf = x.Split('\\');        string[] bBuf = y.Split('\\');        string[] aNameBuf = aBuf[aBuf.Length - 1].Split('.');        string[] bNameBuf = bBuf[bBuf.Length - 1].Split('.');        if (aNameBuf[0].Length > bNameBuf[0].Length)        {            return 1;        }        else if (aNameBuf[0].Length < bNameBuf[0].Length)        {            return -1;        }        else if (aNameBuf[0].Length == bNameBuf[0].Length)        {            return aNameBuf[0].CompareTo(bNameBuf[0]);        }        return 0;    }}