我在串列中有一些資訊(稱為 listLines)。下面的每一行都在一個 List(Of String) 中。
1|This is just a header
3|This is just a footer
2|3456789|0000000|12312312313|BLUE|1|35.00
2|7891230|0000000|45645645655|BLUE|1|22.00
2|7891230|0000000|45645645658|RED|2|13.00
2|3456789|0000000|12312312316|RED|2|45.00
2|3456789|0000000|12312312317|YELLOW|5|-9.00
2|3456789|0000000|12312312315|ORANGE|3|15.00
2|7891230|0000000|45645645659|YELLOW|5|32.00
2|3456789|0000000|12312312314|GREEN|4|-20.00
2|7891230|0000000|45645645656|GREEN|4|39.00
2|7891230|0000000|45645645657|ORANGE|3|-18.50
我正在listLines.sort()串列中按字母順序對其進行排序。以下是我在.sort().
1|This is just a header
2|3456789|0000000|12312312313|BLUE|1|35.00
2|3456789|0000000|12312312314|GREEN|4|-20.00
2|3456789|0000000|12312312315|ORANGE|3|15.00
2|3456789|0000000|12312312316|RED|2|45.00
2|3456789|0000000|12312312317|YELLOW|5|-9.00
2|7891230|0000000|45645645655|BLUE|1|22.00
2|7891230|0000000|45645645656|GREEN|4|39.00
2|7891230|0000000|45645645657|ORANGE|3|-18.50
2|7891230|0000000|45645645658|RED|2|13.00
2|7891230|0000000|45645645659|YELLOW|5|32.00
3|This is just a footer
話雖如此,我需要將此資訊輸出到檔案中。我可以做到這一點。不過我還是有問題。上述資料中有一個序列號,位于您可以看到的所列顏色(RED、BLUE、ETC..)之后的位置 5。它就在最后一個十進制型別的值之前。
我需要進一步排序這個串列,按字母順序排列,因為位置 2 是一個帳號,我想將這些帳號組合在一起。我只是希望它們根據序列號按順序排列。
我正在查看另一個執行緒,試圖弄清楚我該如何做到這一點。我找到了一段代碼,例如listLines.OrderBy(Function(q) q.Substring(35)).ToArray. 我認為如果這是一個固定長度的檔案,這可能會對我有所幫助,但事實并非如此。我在想我可以做一些事情.split()來獲取第 5 條資訊并對其進行排序,但隨后它將取消字母化并將這些行混合起來,因為我不知道如何指定仍然保持字母順序。
現在我正在輸出我的字母串列,如下所示,所以我可以用逗號和雙引號對其進行格式化。
For Each listLine As String In listLines
strPosition = Split(listLine, "|")
Dim i As Integer = 1
Dim iBound As Integer = UBound(strPosition)
Do While (i <= iBound)
strOutputText = strOutputText & Chr(34) & strPosition(i) & Chr(34) & ","
i = 1
Loop
我的主要問題是如何在 .sort() 之后重新排序,然后按順序(位置 5)獲取每個帳戶(位置 1)?或者甚至更好,我怎樣才能同時做這兩個?
uj5u.com熱心網友回復:
該類List(Of T)具有采用委托的Sort方法的多載。Comparison(Of T)我建議你使用它。它允許您撰寫一個方法或 lambda 運算式,該方法或 lambda 運算式將采用兩個專案并以您想要的任何方式比較它們。在這種情況下,您可以這樣做:
Dim items = New List(Of String) From {"1|This Is just a header",
"3|This Is just a footer",
"2|3456789|0000000|12312312313|BLUE|1|35.00",
"2|7891230|0000000|45645645655|BLUE|1|22.00",
"2|7891230|0000000|45645645658|RED|2|13.00",
"2|3456789|0000000|12312312316|RED|2|45.00",
"2|3456789|0000000|12312312317|YELLOW|5|-9.00",
"2|3456789|0000000|12312312315|ORANGE|3|15.00",
"2|7891230|0000000|45645645659|YELLOW|5|32.00",
"2|3456789|0000000|12312312314|GREEN|4|-20.00",
"2|7891230|0000000|45645645656|GREEN|4|39.00",
"2|7891230|0000000|45645645657|ORANGE|3|-18.50"}
items.Sort(Function(x, y)
Dim xParts = x.Split("|"c)
Dim yParts = y.Split("|"c)
'Compare by the first column first.
Dim result = xParts(0).CompareTo(yParts(0))
If result = 0 Then
'Compare by the second column next.
result = xParts(1).CompareTo(yParts(1))
End If
If result = 0 Then
'Compare by the sixth column last.
result = xParts(5).CompareTo(yParts(5))
End If
Return result
End Function)
For Each item In items
Console.WriteLine(item)
Next
如果您更喜歡命名方法,請執行以下操作:
Private Function CompareItems(x As String, y As String) As Integer
Dim xParts = x.Split("|"c)
Dim yParts = y.Split("|"c)
'Compare by the first column first.
Dim result = xParts(0).CompareTo(yParts(0))
If result = 0 Then
'Compare by the second column next.
result = xParts(1).CompareTo(yParts(1))
End If
If result = 0 Then
'Compare by the sixth column last.
result = xParts(5).CompareTo(yParts(5))
End If
Return result
End Function
還有這個:
items.Sort(AddressOf CompareItems)
請注意,這是相當低效的,因為它在每次比較時都會拆分兩個專案。對于一個小串列來說這沒什么大不了的,但是,如果有很多專案,最好將每個專案拆分一次,然后根據這些結果進行排序。
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/459303.html
