Sort list

C#¾ð¿¡¼­ List·Î ¼ÒÆà Çϱâ´Â ½±´Ù.
¾î·Á¿î ÀڷᱸÁ¶³ª ¾Ë°í¸®Áò¿¡ ´ëÇؼ­ ¾ËÇÊ¿ä°¡ ¾ø´Ù.
±×·¡¼­ ¼³¸í º¸´Ù´Â °£´ÜÇÑ ¿¹Á¦·Î ¼³¸íÀ» ÇÏ°Ú´Ù.

ListÀÇ ¾ÆÀÌÅÛÀ¸·Î »ç¿ëÇÒ Å¬·¡½º°¡ ÀÖ´Ù.

    public class Player
    {
        public string Name { get; set; }
        public int Total { get; set; }

        public Player(string name, int total)
        {
            Name = name;
            Total = total;
        }
    }

List¸¦ »ý¼ºÇÏ°í ¾ÆÀÌÅÛÀ» ¸®½ºÆ®¿¡ Ãß°¡ÇÑ´Ù.

            List<Player> list = new List<Player>();
            list.Add(new Player("John", 100));
            list.Add(new Player("Smith", 120));
            list.Add(new Player("Cait", 97));
            list.Add(new Player("Irene", 100));
            list.Add(new Player("Ben", 100));
            list.Add(new Player("Deniel", 88));

¾Æ·¡ ¿¹´Â ¸®½ºÆ®¸¦ ¿À¸§Â÷¼øÀ¸·Î Á¤·ÄÇÏ°í ÀÖ´Ù.

            list.Sort(delegate(Player x, Player y)
            {
                return x.Total.CompareTo(y.Total);
            });

¾Æ·¡ ¿¹´Â ¸®½ºÆ®¸¦ ³»¸²Â÷¼øÀ¸·Î Á¤·ÄÇÏ°í ÀÖ´Ù.

            list.Sort(delegate(Player x, Player y)
            {
                return y.Total.CompareTo(x.Total);
            });

µÎ°³ º¯¼ö·Î Á¤·ÄÇÑ´Ù.

            list.Sort(delegate(Player x, Player y)
            {
                int a = y.Total.CompareTo(x.Total);

                if (a == 0)
                    a = x.Name.CompareTo(y.Name);

                return a;
            });

Àüü ¼Ò½º

using System;
using System.Collections.Generic;
using System.Text;

namespace csharpTest
{
    public class Player
    {
        public string Name { get; set; }
        public int Total { get; set; }

        public Player(string name, int total)
        {
            Name = name;
            Total = total;
        }
    }

    class Test
    {
        public Test()
        {
            List<Player> list = new List<Player>();
            list.Add(new Player("John", 100));
            list.Add(new Player("Smith", 120));
            list.Add(new Player("Cait", 97));
            list.Add(new Player("Irene", 100));
            list.Add(new Player("Ben", 100));
            list.Add(new Player("Deniel", 88));

            list.Sort(delegate(Player x, Player y)
            {
                return x.Total.CompareTo(y.Total);
            });

            Console.Write("<<total ascending sort>>\n");
            list.ForEach(item => Console.Write("{0}\t", item.Total));
           
            list.Sort(delegate(Player x, Player y)
            {
                return y.Total.CompareTo(x.Total);
            });

            Console.Write("\n\n<<total descending sort>>\n");
            list.ForEach(item => Console.Write("{0}\t", item.Total));
           
            list.Sort(delegate(Player x, Player y)
            {
                int a = y.Total.CompareTo(x.Total);

                if (a == 0)
                    a = x.Name.CompareTo(y.Name);

                return a;
            });

            Console.Write("\n\n<<name and total sort>>\n\n");
            list.ForEach(item => Console.Write("{0}, {1}\n", item.Name, item.Total));

            Console.Write("\n");
            Console.ReadKey();
        }
    }
}

½ÇÇà °á°ú

<<total ascending sort>>
88      97      100     100     100     120

<<total descending sort>>
120     100     100     100     97      88

<<name and total sort>>

Smith, 120
Ben, 100
Irene, 100
John, 100
Cait, 97
Deniel, 88