2013年2月8日金曜日

StrCmpLogicalW的な、数字順の並び替え(C#)

個人的には、Regex.ReplacePadLeft(19, '0')で良いと思うのです…

        class Strn_comparer : IComparer {
            #region IComparer メンバ

            public int Compare(string x, string y) {
                int t = Norm(x).CompareTo(Norm(y));
                if (t != 0) return t;
                return x.CompareTo(y);
            }

            static string Norm(string x) {
                return Regex.Replace(x, "\\d+", Pad);
            }

            static string Pad(Match M) {
                return M.Value.PadLeft(19, '0');
            }

            #endregion
        }

C-1 → C-0000000000000000001
C-11 → C-0000000000000000011
C-2 → C-0000000000000000002

これで単純な文字列比較ができるようになります。

しかし、文字列が長くなり、メモリも消費しまくりなきらいは有ります。

いかがなものでしょうか。

0 件のコメント:

コメントを投稿