Well...perhaps if you tried printing from the SortedList instead of from the data source you used to fill it?
At the moment, you use the
students
DataTable to fill the SortedList, but then you loop through the DataTable a second time and print from that, ignoring the list you just created completely...
And why are you adding teh whole table each time?
Try this:
private static void OrderStudentsByMarks(List<DataRow> students)
{
SortedList<int, DataRow> TotalMarkswise = new SortedList<int, DataRow>();
foreach (DataRow dr in students)
{
TotalMarkswise.Add((int)dr["TotalMarks"],dr);
}
Console.WriteLine("ID | Name | Age | Class | Branch | Total Marks");
foreach (var kvp in TotalMarkswise)
{
DataRow dr = kvp.Value;
Console.WriteLine(dr["ID"] + " | " + dr["Name"] + " | " + dr["Age"] + " | " + dr["Class"] + " | " + dr["Branch"] + " | " + dr["TotalMarks"]);
}
Console.ReadLine();
}