Click here to Skip to main content
15,892,298 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
C#
private static void OrderStudentsByMarks(List<DataRow> students)
        {
            SortedList<int, List<DataRow>> TotalMarkswise = new SortedList<int, List<DataRow>>();
            foreach (DataRow dr in students)
            {

                TotalMarkswise.Add(Convert.ToInt16(dr["TotalMarks"]),students);
            }

                Console.WriteLine("ID | Name | Age | Class | Branch | Total Marks");
            foreach (DataRow dr in students )
            {
                Console.WriteLine(dr["ID"] + " | " + dr["Name"] + " | " + dr["Age"] + " | " + dr["Class"] + " | " + dr["Branch"] + " | " + dr["TotalMarks"]);
            }
            Console.ReadLine();
        }
Posted
Comments
Dave Kreskowiak 30-Dec-15 13:57pm    
You might want to tell us what the problem is. It's hard to solve a problem if you can't define it.

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:
C#
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();
        }
 
Share this answer
 
v2
select [col]+[col]+[col] as totalMarks from [TableName] order by totalMarks
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900