Vb.net 按字母顺序排序csv文件Visual Basic
我想将CSV文件转换为数组,然后按字母顺序对数组进行排序Vb.net 按字母顺序排序csv文件Visual Basic,vb.net,Vb.net,我想将CSV文件转换为数组,然后按字母顺序对数组进行排序 Wagner Knox Brennan Shaw Morin Phelps Mccoy Hays Burgess Bowman 这是我的CSV文件,我需要按字母顺序排序 这就是我到目前为止所做的: Dim location As String = "C:\Users\6\Desktop" Dim file As String = "names.csv" Dim sr As New StreamReader
Wagner
Knox
Brennan
Shaw
Morin
Phelps
Mccoy
Hays
Burgess
Bowman
这是我的CSV文件,我需要按字母顺序排序
这就是我到目前为止所做的:
Dim location As String = "C:\Users\6\Desktop"
Dim file As String = "names.csv"
Dim sr As New StreamReader(file & location)
System.Array.Sort(sr)
Console.WriteLine(sr)
它不起作用,因为sr
不是字符串,所以它不会排序
我该怎么做才能使代码正常工作?您基本上都在那里,但是使用file.ReadAllLines(需要导入System.IO)而不是使用streamreader将文件中的所有行读取到数组中会更容易。ReadALlLines将很好地读取文件,将其拆分为每个数组元素一行的数组,关闭文件,清理文件本身,等等
Dim location As String = "C:\Users\6\Desktop"
Dim file As String = "names.csv"
Dim lines = File.ReadAllLines(Path.Combine(location, file))
System.Array.Sort(lines)
Console.WriteLine(string.Join(Environment.NewLine, lines))
始终使用
Path.Combine()
来构建路径,而不是&
操作符Dim arr=Array.Sort(System.IO.File.ReadAllLines(System.IO.Path.Combine(Location,File)))
读取字符串并拆分为数组等等。如果每行都有一个名称。您也可以尝试:Dim arr=System.IO.File.ReadAllLines(System.IO.Path.Combine(Location,File)).OrderBy(Function(x)x)。ToArray
@JQSOFT请在答案中发布答案,而不是在comments@CaiusJard当然,下次。