All pastes #261782 Raw Edit

Stuff

public text v1 · immutable
#261782 ·published 2006-11-29 21:17 UTC
rendered paste body
    class NumericallySortableStringList : List<String>
    {
        /// <summary>
        /// Helper class for sorting the strings ordinally
        /// </summary>
        private class NumericalStringComparer : IComparer<string>
        {
            private Regex _numberRegEx = new Regex(@"([a-zA-Z])(\d+)_(\d+)", RegexOptions.Compiled);
            
            #region IComparer<string> Members
            int IComparer<string>.Compare(string x, string y)
            {
                Match xFileName = _numberRegEx.Match(x);
                Match yFileName = _numberRegEx.Match(y);

                int result = String.Compare(xFileName.Groups[1].Value, yFileName.Groups[1].Value);
                
                if (result != 0)
                    return result;
                else
                    return (int.Parse(xFileName.Groups[2].Value + xFileName.Groups[3].Value)
                        - int.Parse(yFileName.Groups[2].Value + yFileName.Groups[3].Value));
            }
            #endregion
        }

        public void SortOrdinal()
        {
            base.Sort(new OrdinalComparer());
        }
    }