다음을 통해 공유


데이터 정렬(Visual Basic)

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

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

사전순 정렬 작업을 보여 주는 그래픽입니다.

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

메서드

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

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

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

Queryable.ThenBy
그런 다음 내림차순으로排列(ThenByDescending) 2차 정렬을 내림차순으로 수행합니다. Order By …, … Descending Enumerable.ThenByDescending

Queryable.ThenByDescending
리버스 컬렉션에서 요소의 순서를 반대로 바꿉니다. 적용할 수 없습니다. Enumerable.Reverse

Queryable.Reverse

쿼리 식 구문 예제

기본 정렬 예제

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차 오름차순 정렬

다음 예제에서는 LINQ 쿼리에서 절을 사용하여 Order By 배열에서 기본 및 보조 문자열 종류를 수행하는 방법을 보여 줍니다. 문자열은 주로 길이를 기준으로 정렬되고 문자열의 첫 글자를 기준으로 오름차순으로 정렬됩니다.

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차 정렬을 내림차순으로 수행하는 방법을 보여 줍니다. 문자열은 주로 길이를 기준으로 정렬되고 문자열의 첫 글자를 기준으로 보조적으로 정렬됩니다.

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

참고하십시오