如何通过streamreader阅读和排序 [英] How to read by streamreader and sort

查看:78
本文介绍了如何通过streamreader阅读和排序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在制作一个passaparola游戏,在游戏结束时我保存昵称并以流作家作为昵称得分;得分



我正在尝试制作排行榜,但不知道如何在保存时或保存后将它们从最高到最低排序。



还要在标签上显示它们我写了这个;



I'm making a passaparola game , at the end of the game i save nickname and score with stream writer as nickname;score

I'm trying to make a leaderboard but dont know how to sort them highest to lowest at or after saving.

Also to display them on labels i wrote this ;

string[] scoreArray;
       string sc = sr.ReadLine();

       scoreArray = sc.Split(';');
       label2.Text = scoreArray[0];
       label3.Text = scoreArray[1];



在文本文件中写入第一行。无论如何,我如何在标签中对它们进行排序和编写?



我尝试过的方法:



没有尝试任何我不知道如何排序然后写入标签


which writes first line in text file.Anyway how can i sort and write them in labels ?

What I have tried:

didnt try anything i dont know how to sort and then write to labels

推荐答案

最简单的方法是使用Linq和OrderBy。尝试将scoreArray更改为类对象。



Easiest way would be to use Linq and OrderBy. Try changing the scoreArray to a class object.

class HighScore
{
    String Name { get; set; }
    Int32 Score { get; set; }
}

List<highscore> highScores = new List<highscore>();

// add code to read each line in a loop
// add code split the name and score
HighScore score = new HighScore() { Name = name, Score = score };
highScores.Add(score);
// end loop

var orderedList = highScores.OrderBy(x => x.Score);
</highscore></highscore>


这篇关于如何通过streamreader阅读和排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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