Sort Text File lowest value to higest - Programmers Heaven

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

Welcome to the new platform of Programmer's Heaven! We apologize for the inconvenience caused, if you visited us from a broken link of the previous version. The main reason to move to a new platform is to provide more effective and collaborative experience to you all. Please feel free to experience the new platform and use its exciting features. Contact us for any issue that you need to get clarified. We are more than happy to help you.

Sort Text File lowest value to higest

RocksyRocksy Posts: 2Member
Hello

I am new to C# and am very stuck with this particular task. I have been giving a task to create a game of sudoku which I have running and working.
The particular task that I am stuck with is the high score. I am able to write and read back in the text file which has a name, time and date field. What I want to do is sort the scores(times) in order so that the quickest time(lowest) is on top of the list when it loads in.

The code that I have so far only writes and reads in without sorting as follows:

private void btnAdd_Click(object sender, EventArgs e)
{


//user input from 3 text boxes
names[numRecords] = tbName.Text;
time[numRecords] = tbTime.Text;
date[numRecords] = tbDate.Text;
// date[numRecords] = DateTime.Parse(tbDate.Text);
numRecords++;

//save all records to text file

StreamWriter tw = new StreamWriter("storeData.txt", true);
for (int recordNum = 0; recordNum < numRecords; recordNum++)
{

/*write a line of text to the file
as commas are used to separate the data fields they cannot be allowed to
occurr in the data so replace them with alternative character */


time[recordNum] = time[recordNum].Replace(",", "~");
tw.WriteLine("{0}, {1}, {2}", names[recordNum], time[recordNum], date[recordNum]);
}
//close the stream
tw.Close();


}



private void btnLoad_Click(object sender, EventArgs e)
{

lblData.Visible = true;
string[] data; //string array
string input = null;
byte numRecordsRead = 0; //count number of records read
lblData.Text = ""; //clear display




try
{
//create reader and open file
TextReader tr = new StreamReader("storeData.txt", true);
//read line of text
while ((input = tr.ReadLine()) != null) //keep reading while another line exists
{
data = input.Split(',');//split from single string to array of strings

names[numRecordsRead] = data[0];
time[numRecordsRead] = data[1].Replace("~", ","); //undo the character swop
date[numRecordsRead] = data[2]; //convert from string to dateTime
// date[numRecordsRead] = DateTime.Parse(data[2]); //convert from string to dateTime

//output data to label
lblData.Text += names[numRecordsRead].PadLeft(20)
+ time[numRecordsRead].PadLeft(19)
+ date[numRecordsRead].PadLeft(26) + "
";
numRecordsRead++; //complete processing of record


}//while read

tr.Close(); //close the stream
}//try

catch
{
//message to inform user about error
lblData.Text = "error in datafile";
}//catch



Any help in sorting the data in order by lowest to highest time once it is loaded back in would be great. Thanks in advance!!
Sign In or Register to comment.