Natural Sort Order in C#

后端 未结 17 2125
野性不改
野性不改 2020-11-21 04:54

Anyone have a good resource or provide a sample of a natural order sort in C# for an FileInfo array? I am implementing the IComparer interface in

17条回答
  •  予麋鹿
    予麋鹿 (楼主)
    2020-11-21 05:51

    Pure C# solution for linq orderby:

    http://zootfroot.blogspot.com/2009/09/natural-sort-compare-with-linq-orderby.html

    public class NaturalSortComparer : IComparer, IDisposable
    {
        private bool isAscending;
    
        public NaturalSortComparer(bool inAscendingOrder = true)
        {
            this.isAscending = inAscendingOrder;
        }
    
        #region IComparer Members
    
        public int Compare(string x, string y)
        {
            throw new NotImplementedException();
        }
    
        #endregion
    
        #region IComparer Members
    
        int IComparer.Compare(string x, string y)
        {
            if (x == y)
                return 0;
    
            string[] x1, y1;
    
            if (!table.TryGetValue(x, out x1))
            {
                x1 = Regex.Split(x.Replace(" ", ""), "([0-9]+)");
                table.Add(x, x1);
            }
    
            if (!table.TryGetValue(y, out y1))
            {
                y1 = Regex.Split(y.Replace(" ", ""), "([0-9]+)");
                table.Add(y, y1);
            }
    
            int returnVal;
    
            for (int i = 0; i < x1.Length && i < y1.Length; i++)
            {
                if (x1[i] != y1[i])
                {
                    returnVal = PartCompare(x1[i], y1[i]);
                    return isAscending ? returnVal : -returnVal;
                }
            }
    
            if (y1.Length > x1.Length)
            {
                returnVal = 1;
            }
            else if (x1.Length > y1.Length)
            { 
                returnVal = -1; 
            }
            else
            {
                returnVal = 0;
            }
    
            return isAscending ? returnVal : -returnVal;
        }
    
        private static int PartCompare(string left, string right)
        {
            int x, y;
            if (!int.TryParse(left, out x))
                return left.CompareTo(right);
    
            if (!int.TryParse(right, out y))
                return left.CompareTo(right);
    
            return x.CompareTo(y);
        }
    
        #endregion
    
        private Dictionary table = new Dictionary();
    
        public void Dispose()
        {
            table.Clear();
            table = null;
        }
    }
    

提交回复
热议问题