다음을 통해 공유


데이터 정렬

정렬 작업은 하나 이상의 특성을 기반으로 시퀀스의 요소를 정렬합니다. 첫 번째 정렬 기준은 요소에서 기본 정렬을 수행합니다. 2차 정렬 기준을 지정하여 각 기본 정렬 그룹 내에서 요소를 정렬할 수 있습니다.

다음 그림에서는 문자 시퀀스에서의 사전순 정렬 작업에 대한 결과를 보여 줍니다.

LINQ 정렬 작업

데이터를 정렬하는 표준 쿼리 연산자 메서드는 다음 단원에 나열되어 있습니다.

메서드

메서드 이름

설명

C# 쿼리 식 구문

Visual Basic 쿼리 식 구문

추가 정보

OrderBy

값을 오름차순으로 정렬합니다.

orderby

Order By

Enumerable.OrderBy``2

Queryable.OrderBy``2

OrderByDescending

값을 내림차순으로 정렬합니다.

orderby … descending

Order By … Descending

Enumerable.OrderByDescending``2

Queryable.OrderByDescending``2

ThenBy

오름차순으로 2차 정렬을 수행합니다.

orderby …, …

Order By …, …

Enumerable.ThenBy``2

Queryable.ThenBy``2

ThenByDescending

내림차순으로 2차 정렬을 수행합니다.

orderby …, … descending

Order By …, … Descending

Enumerable.ThenByDescending``2

Queryable.ThenByDescending``2

Reverse

컬렉션에서 요소의 순서를 반대로 바꿉니다.

해당 사항 없음.

해당 사항 없음.

Enumerable.Reverse``1

Queryable.Reverse``1

쿼리 식 구문 예제

기본 정렬 예제

기본 오름차순 정렬

다음 예제에서는 LINQ 쿼리에서 orderby(Visual Basic의 Order By) 절을 사용하여 배열의 문자열을 문자열 길이를 기준으로 오름차순 정렬하는 방법을 보여 줍니다.

        Dim words = {"the", "quick", "brown", "fox", "jumps"}

        Dim sortQuery = From word In words 
                        Order By word.Length 
                        Select word

        Dim sb As New System.Text.StringBuilder()
        For Each str As String In sortQuery
            sb.AppendLine(str)
        Next 

        ' Display the results.
        MsgBox(sb.ToString())

        ' This code produces the following output: 

        ' the 
        ' fox 
        ' quick 
        ' brown 
        ' jumps
            string[] words = { "the", "quick", "brown", "fox", "jumps" };

            IEnumerable<string> query = from word in words
                                        orderby word.Length
                                        select word;

            foreach (string str in query)
                Console.WriteLine(str);

            /* This code produces the following output:

                the
                fox
                quick
                brown
                jumps
            */

기본 내림차순 정렬

다음 예제에서는 LINQ 쿼리에서 orderby descending(Visual Basic의 Order By Descending) 절을 사용하여 문자열을 해당 첫 문자를 기준으로 내림차순 정렬하는 방법을 보여 줍니다.

        Dim words = {"the", "quick", "brown", "fox", "jumps"}

        Dim sortQuery = From word In words 
                        Order By word.Substring(0, 1) Descending 
                        Select word

        Dim sb As New System.Text.StringBuilder()
        For Each str As String In sortQuery
            sb.AppendLine(str)
        Next 

        ' Display the results.
        MsgBox(sb.ToString())

        ' This code produces the following output: 

        ' the 
        ' quick 
        ' jumps 
        ' fox 
        ' brown
            string[] words = { "the", "quick", "brown", "fox", "jumps" };

            IEnumerable<string> query = from word in words
                                        orderby word.Substring(0, 1) descending 
                                        select word;

            foreach (string str in query)
                Console.WriteLine(str);

            /* This code produces the following output:

                the
                quick
                jumps
                fox
                brown
            */

2차 정렬 예제

2차 오름차순 정렬

다음 예제에서는 LINQ 쿼리에서 orderby (Visual Basic의 Order By) 절을 사용하여 배열의 문자열에 대한 기본 정렬 및 2차 정렬을 수행하는 방법을 보여 줍니다. 문자열은 기본적으로 길이에 따라 정렬되고 2차적으로는 문자열의 첫 문자에 따라 정렬되며 두 경우 모두 오름차순으로 정렬됩니다.

        Dim words = {"the", "quick", "brown", "fox", "jumps"}

        Dim sortQuery = From word In words 
                        Order By word.Length, word.Substring(0, 1) 
                        Select word

        Dim sb As New System.Text.StringBuilder()
        For Each str As String In sortQuery
            sb.AppendLine(str)
        Next 

        ' Display the results.
        MsgBox(sb.ToString())

        ' This code produces the following output: 

        ' fox 
        ' the 
        ' brown 
        ' jumps 
        ' quick
            string[] words = { "the", "quick", "brown", "fox", "jumps" };

            IEnumerable<string> query = from word in words
                                        orderby word.Length, word.Substring(0, 1)
                                        select word;

            foreach (string str in query)
                Console.WriteLine(str);

            /* This code produces the following output:

                fox
                the
                brown
                jumps
                quick
            */

2차 내림차순 정렬

다음 예제에서는 LINQ 쿼리에서 orderby descending(Visual Basic의 Order By Descending) 절을 사용하여 오름차순으로 기본 정렬을 수행하고 내림차순으로 2차 정렬을 수행하는 방법을 보여 줍니다. 문자열은 기본적으로 길이에 따라 정렬되고 2차적으로는 문자열의 첫 문자에 따라 정렬됩니다.

        Dim words = {"the", "quick", "brown", "fox", "jumps"}

        Dim sortQuery = From word In words 
                        Order By word.Length, word.Substring(0, 1) Descending 
                        Select word

        Dim sb As New System.Text.StringBuilder()
        For Each str As String In sortQuery
            sb.AppendLine(str)
        Next 

        ' Display the results.
        MsgBox(sb.ToString())

        ' This code produces the following output: 

        ' fox 
        ' the 
        ' quick 
        ' jumps 
        ' brown
            string[] words = { "the", "quick", "brown", "fox", "jumps" };

            IEnumerable<string> query = from word in words
                                        orderby word.Length, word.Substring(0, 1) descending 
                                        select word;

            foreach (string str in query)
                Console.WriteLine(str);

            /* This code produces the following output:

                the
                fox
                quick
                jumps
                brown
            */

참고 항목

작업

방법: Join 절 결과의 순서 정렬(C# 프로그래밍 가이드)

방법: LINQ를 사용하여 쿼리 결과 정렬(Visual Basic)

방법: 단어 또는 필드에 따라 텍스트 데이터 정렬 또는 필터링(LINQ)

참조

orderby 절(C# 참조)

Order By 절(Visual Basic)

System.Linq

개념

표준 쿼리 연산자 개요