按字母顺序对文件进行排序 [英] Sort a file alphabetically

查看:43
本文介绍了按字母顺序对文件进行排序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我之前在这里得到了帮助,但我需要更多帮助.该代码按预期工作,但我需要更多功能.

I was helped previously here however I require more assistance. The code works as intended but I need some more function out of it.

我的目标是按字母顺序和从高到低对名称列表进行分数"排序.Mark"帮助我对分数进行排序,但我现在希望按字母顺序对结果进行排序

My objective is to sort a list of names with there "scores" alphabetically and by highest to lowest. 'Mark' helped me sort the scores but I now wish for the results to be sorted alphabetically

我得到的代码:

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

我修改了给我的代码以按字母顺序排序,但程序只是空白,什么也没发生.

I modified the code I was given to sort alphabetically but the program just goes blank and nothing happens.

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

任何有关设法解决此问题的帮助都将非常出色,不胜感激!

Any help on managing to fix this would be brilliant and greatly appreciated!

如果我错过了什么,请告诉我,谢谢!

If I've missed anything let me know, thank you!

按字母顺序排序的原始代码:Dim regex As Regex = New Regex("\w+")Dim sortedNames =From line In File.ReadLines("S:\class" & CName & ".rtf")让 match = regex.Match(line, "Name: (\w+)")匹配成功的地方按 CStr(match.Groups(1).Value) 升序排序选择线对于 sortedNames 中的每一行Console.WriteLine(行)下一个

The original code working for sorting in alphabetical order: Dim regex As Regex = New Regex("\w+") Dim sortedNames = From line In File.ReadLines("S:\class" & CName & ".rtf") Let match = regex.Match(line, "Name: (\w+)") Where match.Success Order By CStr(match.Groups(1).Value) Ascending Select line For Each line In sortedNames Console.WriteLine(line) Next

推荐答案

我会使用不同的方法:如果你想要一个与值(分数,作为整数)相关联的字符串(名称),我认为最好拟合对象将是字典:

I would use a different approach: if you want a String (a name) associated with a value (the score, as an Integer), I think the best fitting object would be a Dictionary:

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

现在名字和分数是关联的,你可以按分数排序:

Now the name and the score are linked, and you can sort them by score:

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

这篇关于按字母顺序对文件进行排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆