Sort Text File lowest value to higest


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);

//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


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

//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

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

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.

Howdy, Stranger!

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