.netナチュラルソート方式

3693 ワード

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

namespace ConsoleApplication12

{

    class Program

    {

        static void Main(string[] args)

        {



            string[] str = new string[] { "A1", "A2", "A10" };

            Array.Sort(str, new CustomComparer());

            for (int i = 0; i < str.Length; i++)

                Console.WriteLine(str[i]);

        }

    }



    public class CustomComparer : System.Collections.IComparer

    {

        public int Compare(object x, object y)

        {

            string s1 = (string)x;

            string s2 = (string)y;

            if (s1.Length > s2.Length) return 1;

            if (s1.Length < s2.Length) return -1;

            for (int i = 0; i < s1.Length; i++)

            {

                if (s1[i] > s2[i]) return 1;

                if (s1[i] < s2[i]) return -1;

            }

            return 0;

        }

    }





}