Share via


데이터 정렬

업데이트: 2007년 11월

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

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

LINQ 정렬 작업

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

메서드

메서드 이름

설명

C# 쿼리 식 구문

Visual Basic 쿼리 식 구문

추가 정보

OrderBy

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

orderby

Order By

Enumerable.OrderBy

Queryable.OrderBy

OrderByDescending

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

orderby … descending

Order By … Descending

Enumerable.OrderByDescending

Queryable.OrderByDescending

ThenBy

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

orderby …, …

Order By …, …

Enumerable.ThenBy

Queryable.ThenBy

ThenByDescending

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

orderby …, … descending

Order By …, … Descending

Enumerable.ThenByDescending

Queryable.ThenByDescending

Reverse

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

적용할 수 없음.

적용할 수 없음.

Enumerable.Reverse<TSource>

Queryable.Reverse<TSource>

쿼리 식 구문 예제

기본 정렬 예제

기본 오름차순 정렬

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

Dim words() As String = {"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 쿼리에서 orderbydescending(Visual Basic의 Order By Descending) 절을 사용하여 문자열을 해당 첫 문자를 기준으로 내림차순 정렬하는 방법을 보여 줍니다.

Dim words() As String = {"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() As String = {"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 쿼리에서 orderbydescending(Visual Basic의 Order By Descending) 절을 사용하여 오름차순으로 기본 정렬을 수행하고 내림차순으로 2차 정렬을 수행하는 방법을 보여 줍니다. 문자열은 기본적으로 길이에 따라 정렬되고 2차적으로는 문자열의 첫 문자에 따라 정렬됩니다.

Dim words() As String = {"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
*/

데이터 정렬 방법에 대한 추가 정보

참고 항목

개념

표준 쿼리 연산자 개요

참조

orderby 절(C# 참조)

Order By 절(Visual Basic)

System.Linq