다음을 통해 공유


데이터 정렬(Visual Basic)

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

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

Graphic that shows an alphabetical sort operation.

다음 섹션에는 데이터를 정렬하는 표준 쿼리 연산자 메서드가 나와 있습니다.

메서드

메서드 이름 설명 Visual Basic 쿼리 식 구문 추가 정보
OrderBy 값을 오름차순으로 정렬합니다. Order By Enumerable.OrderBy

Queryable.OrderBy
OrderByDescending 값을 내림차순으로 정렬합니다. Order By … Descending Enumerable.OrderByDescending

Queryable.OrderByDescending
ThenBy 2차 정렬을 오름차순으로 수행합니다. Order By …, … Enumerable.ThenBy

Queryable.ThenBy
ThenByDescending 2차 정렬을 내림차순으로 수행합니다. Order By …, … Descending Enumerable.ThenByDescending

Queryable.ThenByDescending
Reverse 컬렉션에서 요소의 순서를 반대로 바꿉니다. 해당 없음. Enumerable.Reverse

Queryable.Reverse

쿼리 식 구문 예제

1차 정렬 예제

1차 오름차순 정렬

다음 예제에서는 LINQ 쿼리에 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

1차 내림차순 정렬

다음 예제에서는 LINQ 쿼리에 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

2차 정렬 예제

2차 오름차순 정렬

다음 예제에서는 LINQ 쿼리에 Order By 절을 사용하여 배열의 문자열에 대해 1차 및 2차 정렬을 수행하는 방법을 보여 줍니다. 문자열은 길이의 오름차순으로 1차 정렬된 다음 문자열 첫 글자의 오름차순으로 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

2차 내림차순 정렬

다음 예제에서는 LINQ 쿼리에 Order By Descending 절을 사용하여 1차 정렬을 오름차순으로 수행한 다음 2차 정렬을 내림차순으로 수행하는 방법을 보여 줍니다. 문자열은 길이순으로 1차 정렬된 다음 문자열 첫 글자순으로 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

참고 항목