C#

Moderators: None (Apply to moderate this forum)
Number of threads: 2720
Number of posts: 5746

This Forum Only
Post New Thread
Single Post View       Linear View       Threaded View      f

Report
Sort Text File lowest value to higest Posted by Rocksy on 19 Apr 2012 at 4:10 PM
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) + "\n";
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!!
Report
Re: Sort Text File lowest value to higest Posted by AnnieCalvert on 2 Jul 2012 at 11:17 PM
You should use Row.Update() method .This method is used to change row position during sorting.When event-driven model is used , the grid gets relevant Property Changed and List Changed notifications.
http://www.dapfor.com/en/net-suite/net-grid/tutorial/data-sorting
Report
Re: Sort Text File lowest value to higest Posted by kattypier on 2 Nov 2012 at 1:08 AM
more help you can visit dapfor which is provide good tutorial for sorting and event driven dapfor. com



 

Recent Jobs