1명 중 1명이 도움이 되는 것으로 평가 - 이 항목 평가

List<T>.Sort 메서드 (Comparison<T>)

업데이트: 2007년 11월

지정된 System.Comparison<T>을 사용하여 전체 List<T>의 요소를 정렬합니다.

네임스페이스:  System.Collections.Generic
어셈블리:  mscorlib(mscorlib.dll)
public void Sort(
	Comparison<T> comparison
)
public void Sort(
	Comparison<T> comparison
)
public function Sort(
	comparison : Comparison<T>
)

매개 변수

comparison
형식: System.Comparison<T>
요소를 비교할 때 사용할 System.Comparison<T>입니다.
예외상황
ArgumentNullException

comparisonnull인 경우

ArgumentException

정렬하는 동안comparison 구현에서 오류가 발생한 경우 예를 들어, comparison는 항목을 항목 자체와 비교할 때 0을 반환하지 않을 수 있습니다.

comparison이 제공되면 List<T>의 요소는 대리자가 나타내는 메서드를 사용하여 정렬됩니다.

comparisonnull이면 ArgumentNullException이 throw됩니다.

이 메서드는 QuickSort 알고리즘을 사용하는 Array.Sort를 사용합니다. 이 구현에서는 불안정한 정렬을 수행합니다. 즉, 두 개의 같은 요소가 있을 경우 이들 요소의 순서가 유지되지 않을 수 있습니다. 이와 반대로 안정된 정렬은 동일한 요소의 순서를 그대로 유지합니다.

평균적으로 이 메서드는 O(n log n) 연산이며, 여기서 nCount입니다. 최악의 경우 이 메서드는 O(n ^ 2) 연산입니다.

다음 코드 예제에서는 Sort(Comparison<T>) 메서드 오버로드를 보여 줍니다.

이 코드 예제에서는 문자열에 대한 대체 비교 메서드 CompareDinosByLength를 정의합니다. 이 메서드는 다음과 같이 작동합니다. 먼저 비교 대상이 null인지 테스트하고 null 참조를 null이 아닌 값보다 작은 것으로 처리합니다. 두 번째로, 문자열 길이를 비교하고 긴 문자열을 더 큰 것으로 간주합니다. 세 번째로, 길이가 같으면 일반 문자열 비교를 사용합니다.

이 예제에서는 문자열의 List<T>을 만들어 특정한 순서 없이 문자열 4개로 채웁니다. 이 목록에는 빈 문자열과 null 참조도 포함됩니다. 목록을 표시하고 CompareDinosByLength 메서드를 나타내는 Comparison<T> 제네릭 대리자를 사용하여 정렬한 다음 목록을 다시 표시합니다.

using System;
using System.Collections.Generic;

public class Example
{
    private static int CompareDinosByLength(string x, string y)
    {
        if (x == null)
        {
            if (y == null)
            {
                // If x is null and y is null, they're
                // equal. 
                return 0;
            }
            else
            {
                // If x is null and y is not null, y
                // is greater. 
                return -1;
            }
        }
        else
        {
            // If x is not null...
            //
            if (y == null)
                // ...and y is null, x is greater.
            {
                return 1;
            }
            else
            {
                // ...and y is not null, compare the 
                // lengths of the two strings.
                //
                int retval = x.Length.CompareTo(y.Length);

                if (retval != 0)
                {
                    // If the strings are not of equal length,
                    // the longer string is greater.
                    //
                    return retval;
                }
                else
                {
                    // If the strings are of equal length,
                    // sort them with ordinary string comparison.
                    //
                    return x.CompareTo(y);
                }
            }
        }
    }

    public static void Main()
    {
        List<string> dinosaurs = new List<string>();
        dinosaurs.Add("Pachycephalosaurus");
        dinosaurs.Add("Amargasaurus");
        dinosaurs.Add("");
        dinosaurs.Add(null);
        dinosaurs.Add("Mamenchisaurus");
        dinosaurs.Add("Deinonychus");
        Display(dinosaurs);

        Console.WriteLine("\nSort with generic Comparison<string> delegate:");
        dinosaurs.Sort(CompareDinosByLength);
        Display(dinosaurs);

    }

    private static void Display(List<string> list)
    {
        Console.WriteLine();
        foreach( string s in list )
        {
            if (s == null)
                Console.WriteLine("(null)");
            else
                Console.WriteLine("\"{0}\"", s);
        }
    }
}

/* This code example produces the following output:

"Pachycephalosaurus"
"Amargasaurus"
""
(null)
"Mamenchisaurus"
"Deinonychus"

Sort with generic Comparison<string> delegate:

(null)
""
"Deinonychus"
"Amargasaurus"
"Mamenchisaurus"
"Pachycephalosaurus"
 */


Windows Vista, Windows XP SP2, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP Starter Edition, Windows Server 2003, Windows Server 2000 SP4, Windows Millennium Edition, Windows 98, Windows CE, Windows Mobile for Smartphone, Windows Mobile for Pocket PC, Xbox 360

.NET Framework 및 .NET Compact Framework에서 모든 플랫폼의 전체 버전을 지원하지는 않습니다. 지원되는 버전의 목록을 보려면 .NET Framework 시스템 요구 사항을 참조하십시오.

.NET Framework

3.5, 3.0, 2.0에서 지원

.NET Compact Framework

3.5, 2.0에서 지원

XNA Framework

2.0, 1.0에서 지원
이 정보가 도움이 되었습니까?
(1500자 남음)

커뮤니티 추가 항목

추가
Microsoft는 MSDN 웹 사이트에 대한 귀하의 의견을 이해하기 위해 온라인 설문 조사를 진행하고 있습니다. 참여하도록 선택하시면 MSDN 웹 사이트에서 나가실 때 온라인 설문 조사가 표시됩니다.

참여하시겠습니까?
© 2013 Microsoft. All rights reserved.