다차원 배열을 정렬하려면 단일 차원 배열을 정렬하는 것과 완전히 동일한 절차를 사용할 수 있습니다.
배열을 반복하고 인접한 두 멤버의 순서가 잘못되어있는 경우 스 와이프합니다.
는, 그래서 다중 차원 배열과 같은
Dim Array(12, 4) String
이 당신이 예를 들어, 마지막 열을 기준으로 배열을 주문한다고 가정 상상해보십시오.
For index1 = 0 to Array.length - 1
For index2 = 0 to Array.length - 2
If Array(index2, 4) > Array(index2 + 1, 4) Then
// this sorts the last column
Temp4 = Array(index2, 4)
Array(index2, 4) = Array(index2 + 1, 4)
Array(index2 + 1, 4) = Temp4
// and we repeat this swopping pattern for each of the other columns, so they are all sorted in the same way
Temp3 = Array(index2, 3)
Array(index2, 3) = Array(index2 + 1, 3)
Array(index2 + 1, 3) = Temp3
Temp2 = Array(index2, 2)
Array(index2, 2) = Array(index2 + 1, 2)
Array(index2 + 1, 2) = Temp2
Temp1 = Array(index2, 1)
Array(index2, 1) = Array(index2 + 1, 1)
Array(index2 + 1, 1) = Temp1
Temp0 = Array(index2, 0)
Array(index2, 0) = Array(index2 + 1, 0)
Array(index2 + 1, 0) = Temp0
Next
Next
이제 모든 열은 마지막 열로 정렬됩니다.
네 개의 열이 문자열이고 한 개의 열이 숫자 인 경우 정렬 할 수있는 방법이 궁금 할 수 있습니다. 그리고 숫자 열을 기준으로 정렬하려는 경우를 생각해보십시오.
당신은 할 수있는이
For index1 = 0 to Array.length - 1
For index2 = 0 to Array.length - 2
If CSng(Array(index2, 4)) > CSng(Array(index2 + 1, 4)) Then
// this sorts the last column
Temp4 = Array(index2, 4)
Array(index2, 4) = Array(index2 + 1, 4)
Array(index2 + 1, 4) = Temp4
// and we repeat this swopping pattern for each of the other columns, so they are all sorted in the same way
Temp3 = Array(index2, 3)
Array(index2, 3) = Array(index2 + 1, 3)
Array(index2 + 1, 3) = Temp3
Temp2 = Array(index2, 2)
Array(index2, 2) = Array(index2 + 1, 2)
Array(index2 + 1, 2) = Temp2
Temp1 = Array(index2, 1)
Array(index2, 1) = Array(index2 + 1, 1)
Array(index2 + 1, 1) = Temp1
Temp0 = Array(index2, 0)
Array(index2, 0) = Array(index2 + 1, 0)
Array(index2 + 1, 0) = Temp0
Next
Next
내가 행한 모든 후, 단일로 문자열에서 마지막 컬럼에 데이터 형식을 변환 이전과 인접한 값을 비교하는 것입니다.
그래서 이것은 다차원 배열을 정렬하는 아주 기본적인 방법이며, 문자열과 숫자가 포함 된 혼합 배열에도 적용됩니다.
왜 ['Array.Sort'] (http://msdn.microsoft.com/en-us/library/system.array.sort (v = vs.110) .aspx)를 사용할 수 없습니까? –
작업은 정렬 함수를 만드는 것입니다 .. –
여기를 참고하십시오 http://en.wikipedia.org/wiki/Bubble_sort – Teejay