[英]vba How to Bubble Sort with two Criteria
我有一個這樣的txt數組:
我已經設法用冒泡排序對該數組進行排序,選擇了第二組數字“ 001,001,002,002,003 ....”,但我也想對第一組數字進行排序。 結果如下:
關於如何構造冒泡排序的任何想法嗎? 我是否需要在我的常規氣泡排序內的新For循環中使用?
當前代碼(這將僅基於第二組數字排序)。 104 =最后一組數字的位置
For i = 1 To UbndCellDataExcel - 1
For j = i + 1 To UbndCellDataExcel
If Mid(CellDataExcel(i), 104, 3) > Mid(CellDataExcel(j), 104, 3) Then
strTemp = CellDataExcel(i)
CellDataExcel(i) = CellDataExcel(j)
CellDataExcel(j) = strTemp
End If
Next j
Next i
解決方案的關鍵是比較功能:
這樣做有兩種主要方法:第一種也是最簡單的方法-創建一個新數字並按其排序
convert "Unique text |05||001|" to "00105"
convert "Unique text |04||002|" to "00204"
00204>00105 so "Unique text |04||002|" > "Unique text |05||001|"
更正確,更復雜的是簡單地做兩個比較:
Function compare (ByVal i As String,ByVal j As String)
i1=getParam(1,i)
i2=getParam(2,i)
j1=getParam(1,j)
j1=getParam(2,j)
if (i1>j1) return 1
if (i2<j2) return -1
if (j1>j1) return 1
if (j2<j2) return -1
return 0
其中getParam
是采用“唯一文本| 04 || 002 |”的函數 並返回“ 04”或“ 002”。
謝謝謝謝! 這是我現在根據您的想法實現的轉換:
convert "Unique text |05||001|" to "00105"
convert "Unique text |04||002|" to "00204"
我的代碼現在看起來像這樣(100 =第一組數字的位置,而104是第二組數字):
For i = 1 To UbndCellDataExcel - 1
For j = i + 1 To UbndCellDataExcel
If Mid(CellDataExcel(i), 104, 3) & Mid(CellDataExcel(i), 100, 2) > Mid(CellDataExcel(j), 104, 3) & Mid(CellDataExcel(j), 100, 2) Then 'Sorter basert på OUnr
strTemp = CellDataExcel(i)
CellDataExcel(i) = CellDataExcel(j)
CellDataExcel(j) = strTemp
End If
Next j
Next i
'Mid(CellDataExcel(i), 104, 3) & Mid(CellDataExcel(i), 100, 2) returns Format: "00105"
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.