Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C#ASP.NET.NET
I've a list of students, the list contains student name, roll number and subjects like english ,math, phy etc. Now i want to calculate the position of each student in the list for the subjects english and other subjects.
 
Like i have 45 students in class and list contains 45 students, for each student i want to calculate his position in the relevant subject.
 

  List<Student> student = new List<Student>();
 

Any to help.
Posted 19-Dec-12 7:49am
Edited 19-Dec-12 7:57am
RyanDev90.8K
v2
Comments
DinoRondelly at 19-Dec-12 13:52pm
   
Have you looked through the methods of list? http://msdn.microsoft.com/en-us/library/6sh2ey19.aspx
partner99 at 19-Dec-12 14:02pm
   
i have got a list of students with all subjects and their marks, now on the list of students i want give postion to the each student in each subject? i looked into the list methods but it did not helped in any case.
joshrduncan2012 at 19-Dec-12 13:52pm
   
What have you done to accomplish this task so far? Where are you stuck?
partner99 at 19-Dec-12 13:56pm
   
i have got a list of students with all subjects and their marks, now on the list of students i want give postion to the each student in each subject? i looked into the list methods but it did not helped in any case.
Sk. Tajbir at 19-Dec-12 13:54pm
   
What do you mean by position for the subjects ? Do you have any column/property for storing Marks of the student?
partner99 at 19-Dec-12 13:57pm
   
by postion i mean first, second, third . . . . 11th in subjects by marks obtained in each subjects, yes i have separate columns for subjects marks , like englishobtainedMarks, MathObtainedMarks etc . . .
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

Use the OrderBy method and a foreach to set the Rank.
 
 
students.OrderByDescending(x=>x.English).ToList().ForEach(s=>s.PositionInEnglish=rank++)
 
etc...
  Permalink  
v4
Comments
partner99 at 19-Dec-12 14:05pm
   
but i have another column for positionInEnglish, positionInMath etc and i want to place the position in that column also. your solution will just sort list by obtained marks in subjects either in english or math or physic
BC @ CV at 19-Dec-12 14:07pm
   
Are you then trying to sort by the combined GPA?
partner99 at 19-Dec-12 14:28pm
   
no i'm not calculating the gpa
BC @ CV at 19-Dec-12 14:30pm
   
Ok if you want to update a property in each item in your list just iterate like so.... int rank = 1; foreach(Student s in students.OrderByDescending(x =>x.EnglishGrade)) s.PositionInEnglish = rank++;
partner99 at 19-Dec-12 14:38pm
   
can you have a look here please; http://www.codeproject.com/Answers/512557/howplustopluscalculateplusthepluspositionplusofplu#answer2
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

You could use the Linq OrderBy method:
        class student
            {
            public int marks;
            public student(int mark) { marks = mark; }
            }
        void myButton_Click(object sender, EventArgs e)
            {
            List<student> list = new List<student>();
            list.Add(new student(7));
            list.Add(new student(3));
            list.Add(new student(5));
            list.Add(new student(88));
            list.Add(new student(2));
            list.Add(new student(963));
            list.Add(new student(6));
            list = list.OrderBy(s => s.marks).ToList();
            }
  Permalink  
Comments
partner99 at 19-Dec-12 14:21pm
   
this is just sorting, but what if we have like this; class student { public string positionInEnglish;//position to be calculated and placed in this field for each student public string positionInMath;//position to be calculated and placed in this field for each student public int EnglishObtainedMarks; public int mathObtainedMarks; public student(int engObtMarks,int mathObtMarks) { EnglishObtainedMarks= engObtMarks; mathObtainedMarks = mathObtMarks; } } list.Add(new student(7,30)); // position of student = 3rd list.Add(new student(3,40));// position of student = 6th list.Add(new student(5,60));// position of student = 5th list.Add(new student(88,70));// position of student = 2nd list.Add(new student(2,5));// position of student = 7th list.Add(new student(963,90));// position of student = 1st list.Add(new student(6,34));// position of student = 4th list.Add(new student(963,343));// position of student = 1st list.Add(new student(963,343));// position of student = 1st
OriginalGriff at 19-Dec-12 14:50pm
   
Then you just change the property you are sorting by: list.OrderBy(s => s.EnglishObtainedMarks) or list.OrderBy(s => s.mathObtainedMarks)
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 3

void Main()
{
List list = new List();
list.Add(new Student(7,30)); // position of student = 3rd
list.Add(new Student(3,40));// position of student = 6th
list.Add(new Student(5,60));// position of student = 5th
list.Add(new Student(88,70));// position of student = 2nd
list.Add(new Student(2,5));// position of student = 7th
list.Add(new Student(963,90));// position of student = 1st
list.Add(new Student(6,34));// position of student = 4th
list.Add(new Student(963,343));// position of student = 1st
list.Add(new Student(963,343));// position of student = 1st

list.OrderByDescending (c => c.EngPts).ThenByDescending (c => c.MathPts);
//at this point your list should be sorted like so
/*
963 343
963 343
963 90
88 70
7 30
6 34
5 60
3 40
2 5
which seems to match the ranking you are trying to get
*/
}
 
class Student
{
//public string positionInEnglish;//position to be calculated and placed in this field for each student
//public string positionInMath;//position to be calculated and placed in this field for each student

public int EngPts{get;set;}
public int MathPts{get;set;}

public Student(int engPts,int mathPts)
{
EngPts = engPts;
MathPts = mathPts;
}
}
 
this is my 2cents.
  Permalink  

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

  Print Answers RSS
Your Filters
Interested
Ignored
     
0 Guruprasad.K.Basavaraju 400
1 Sergey Alexandrovich Kryukov 301
2 Shai Vashdi 240
3 OriginalGriff 155
4 Tadit Dash 135
0 Sergey Alexandrovich Kryukov 9,225
1 OriginalGriff 5,395
2 Peter Leow 4,100
3 Maciej Los 3,540
4 Abhinav S 3,333


Advertise | Privacy | Mobile
Web01 | 2.8.140415.2 | Last Updated 19 Dec 2012
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Use
Layout: fixed | fluid