Vb.net 按字母顺序对文件排序
我以前得到过帮助,但是我需要更多的帮助。代码按预期工作,但我需要更多的功能 我的目标是按照字母顺序和从高到低的顺序对一系列有“分数”的名字进行排序马克帮助我对分数进行排序,但我现在希望结果按字母顺序排序 给我的代码是:Vb.net 按字母顺序对文件排序,vb.net,Vb.net,我以前得到过帮助,但是我需要更多的帮助。代码按预期工作,但我需要更多的功能 我的目标是按照字母顺序和从高到低的顺序对一系列有“分数”的名字进行排序马克帮助我对分数进行排序,但我现在希望结果按字母顺序排序 给我的代码是: Dim regex As Regex = New Regex("\d+") Dim sortedScores = From line In File.ReadLines("S:\class" & CName & ".rtf") Le
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
它神奇地工作
文件看起来像
我修改了给我的代码,按字母顺序排序,但程序只是变为空白,什么也没发生
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+”)
模糊分类名称=
从文件.ReadLines中的行(“S:\class”&CName&“.rtf”)
让match=regex.match(第行,“名称:(\w+)”)
哪里有比赛,成功吗
按CStr排序(匹配组(1).值)递增
选择行
对于分拣数据名称中的每一行
控制台写入线(行)
接下来我将使用另一种方法:如果您想要一个与值(分数,作为整数)关联的字符串(名称),我认为最合适的对象应该是字典:
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
我会使用另一种方法:如果你想要一个字符串(一个名称)与一个值(分数,作为一个整数)相关联,我认为最合适的对象应该是字典:
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
嗯,看起来不错,但我有一个奇怪的错误?”Sort'不是'System.Collections.Generic.Dictionary(字符串、整数的).KeyCollection'的成员。我使用VisualStudio作为IDE,所以我假设在某个地方需要一行代码?我刚刚编辑了一些细节。无论如何,我无法访问您的rtf文件,因此无法运行代码。嗯,我似乎无法使其正常工作,当我尝试每种排序方式时,都会出现以下错误:“无法将类型为“System.IO.ReadLinesIterator”的对象强制转换为类型为“System.String[]”错误似乎在这里:Dim lines()作为String=file.ReadLines(“S:\class”&CName&“.rtf”)如果这是不可执行的,那么我最初使用的方法是否可以修改为按名称排序?True。改为使用此
文件.ReadAllLines(“S:\class”&CName&“.rtf”)
。我在没有文件的情况下测试它,并做了一些编辑。它对我有用,如果你在使用它时仍然有问题,请告诉我。对不起,我抓到了一些东西吃,我不确定在哪里使用File.ReadAllLines,代码不是已经这样做了吗?嗯,看起来不错,但是,我有一个奇怪的错误?”Sort'不是'System.Collections.Generic.Dictionary(字符串、整数的).KeyCollection'的成员。我使用VisualStudio作为IDE,所以我假设在某个地方需要一行代码?我刚刚编辑了一些细节。无论如何,我无法访问您的rtf文件,因此无法运行代码。嗯,我似乎无法使其正常工作,当我尝试每种排序方式时,都会出现以下错误:“无法将类型为“System.IO.ReadLinesIterator”的对象强制转换为类型为“System.String[]”错误似乎在这里:Dim lines()作为String=file.ReadLines(“S:\class”&CName&“.rtf”)如果这是不可执行的,那么我最初使用的方法是否可以修改为按名称排序?True。改为使用此文件.ReadAllLines(“S:\class”&CName&“.rtf”)
。我在没有文件的情况下测试它,并做了一些编辑。它对我有用,如果你在使用它时仍然有困难,请告诉我。对不起,我抓到了一些东西吃,我不确定在哪里使用File.ReadAllLines,代码不是已经这样做了吗?