Reputation: 41
I am making a quiz game for my computing coursework and I have completed the quiz part of my application and I am currently moving on to my leader board but to do that I need to sort my scores text file which contains the users name, score and time but I am now trying to retrieve these data and display them in a table.
The users' name score and time saves fine into the scores text file but to display these details I first need to order these details by the score.
My test file is organised like this:
username,score,time
userName1,33,12
userName2,-55,33
userName3,34,2
userName4,23,27
userName5,63,72
This is the code that I am currently using but this only works if I have the data in the text file sorted first.
string[] readFile = File.ReadAllLines(file).ToArray();
for (int i = 0; i < 5; i++)
{
string[] userDetails = File.ReadAllLines(file).ToArray();
string username = userDetails[0];
string score = userDetails[1];
// Apply the text of lblUsername1-5 to be what the names
// of the top 5 scorers are in the file.
lblUsername1.Text = userDetails[0].Split(',')[0];
lblUsername2.Text = userDetails[1].Split(',')[0];
lblUsername3.Text = userDetails[2].Split(',')[0];
lblUsername4.Text = userDetails[3].Split(',')[0];
lblUsername5.Text = userDetails[4].Split(',')[0];
// Apply the text of lblScore1-5 to be what the scores
// of the top 5 scorers are in the file.
lblScore1.Text = userDetails[0].Split(',')[1];
lblScore2.Text = userDetails[1].Split(',')[1];
lblScore3.Text = userDetails[2].Split(',')[1];
lblScore4.Text = userDetails[3].Split(',')[1];
lblScore5.Text = userDetails[4].Split(',')[1];
}
So if some one could help me to sort the data in my scores ext file that would be great. Thanks in advance.
Upvotes: 2
Views: 1027
Reputation: 1044
You can use linq to sort data from your file
string[][] userDetails = File.ReadAllLines(file).Select(s => s.Split(',')).OrderBy(arr => int.TryParse(arr[1], out int result) ? result : 0)).Take(5).ToArray();
lblUsername1.Text = userDetails[0][0];
lblUsername2.Text = userDetails[1][0];
lblUsername3.Text = userDetails[2][0];
lblUsername4.Text = userDetails[3][0];
lblUsername5.Text = userDetails[4][0];
// Apply the text of lblScore1-5
// to be what the scores of the top 5 scorers are in the file.
lblScore1.Text = userDetails[0][1];
lblScore2.Text = userDetails[1][1];
lblScore3.Text = userDetails[2][1];
lblScore4.Text = userDetails[3][1];
lblScore5.Text = userDetails[4][1];``
Upvotes: 1
Reputation: 4323
You should use objects to manage this. Your class should be a user with properties, se below.
Now you have full control on sorting and managing of your objects
using System.Collections.Generic;
using System.Linq;
public class User
{
public string Name { get; set; }
public int Score { get; set; }
public int Time { get; set; }
}
class Program
{
public static void Main(string[] args)
{
//This you get from file, no need for this in your code
string[] fromFile = new string[5]
{ "userName1,33,12", "userName2,-55,33", "userName3,34,2", "userName4,23,27", "userName5,63,72" };
List<User> users = new List<User>();
foreach (string line in fromFile)
{
string[] splitLine = line.Split(',');
users.Add(new User() { Name = splitLine[0], Score = int.Parse(splitLine[1]), Time = int.Parse(splitLine[2]) });
}
foreach (User oneUser in users.OrderByDescending(x => x.Score))
{
//Here the store to file or what you want to do
}
}
}
Upvotes: 0