我之前得到了帮助here但我需要更多帮助。代码按预期工作,但我还需要更多功能。
我的目标是按字母顺序排列名称列表,并按从最高到最低排序。 '马克'帮助我对分数进行排序,但我现在希望结果按字母顺序排序
我给出的代码:
Dim regex As Regex = New Regex("\d+")
Dim sortedScores =
From line In File.ReadLines("S:\class" & CName & ".rtf")
Let match = regex.Match(line, "Sore: (\d+)")
Where match.Success
Order By CInt(match.Groups(1).Value) Descending
Select line
For Each line In sortedScores
Console.WriteLine(line)
Next
它神奇地工作。
该文件看起来像this
我修改了我给出的代码按字母顺序排序,但程序只是空白而没有任何反应。
Dim regex As Regex = New Regex("\d+")
Dim sortedScores =
From line In File.ReadLines("S:\class" & CName & ".rtf")
Let match = regex.Match(line, "Name: (\d+)")
Where match.Success
Order By CStr(match.Groups(1).Value) Descending
Select line
For Each line In sortedScores
Console.WriteLine(line)
Next
任何有关设法解决这个问题的帮助都会非常棒,非常感谢!
如果我错过任何事情请告诉我,谢谢!
编辑:
原始代码按字母顺序排序: Dim regex As Regex = New Regex(“\ w +”) Dim sortedNames = 来自File.ReadLines中的行(“S:\ class”& CName&“.rtf”) 设match = regex.Match(行,“名称:(\ w +)”) 匹配。成功 按CStr排序(match.Groups(1).Value)升序 选择行 对于每行在sortedNames中 Console.WriteLine(行) 下一步
答案 0 :(得分:0)
我会使用不同的方法:如果你想要一个与一个值相关联的String(一个名字)(得分,作为一个整数),我认为最合适的对象将是一个词典:
Dim dict As New Dictionary(Of String, Integer) 'Key: Name, Value: Score
Dim lines() As String = File.ReadAllLines("S:\class" & CName & ".rtf")
For Each line As String In lines
Dim lineParts() As String = line.Split("¦")
Dim name As String = lineParts(0).Replace("Name : ","")
Dim score As String = lineParts(1).Replace("Score : ","")
dict.Add(name, Integer.Parse(score))
Next
现在名称和分数已关联,您可以按分数对其进行排序:
Dim pairList As List(Of KeyValuePair(Of String, Integer)) = (From entry In dict Order By entry.Value Ascending Select entry).ToList()
For Each pair As KeyValuePair(Of String, Integer) In pairList
Console.WriteLine("Name: {0}, Score: {1}", pair.Key, pair.Value)
Next
......或按名称:
Dim sortedNames = dict.Keys.ToList().Sort()
For Each name As String In sortedNames
Console.WriteLine("Name: {0}, Score: {1}", name, dict(name))
Next