Click here to Skip to main content
15,893,161 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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.


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



Any to help.
Posted
Updated 19-Dec-12 7:57am
v2
Comments
DinoRondelly 19-Dec-12 13:52pm    
Have you looked through the methods of list?

http://msdn.microsoft.com/en-us/library/6sh2ey19.aspx
Zia Ullah Khan 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 19-Dec-12 13:52pm    
What have you done to accomplish this task so far? Where are you stuck?
Zia Ullah Khan 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 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?

Use the OrderBy method and a foreach to set the Rank.

C#
students.OrderByDescending(x=>x.English).ToList().ForEach(s=>s.PositionInEnglish=rank++)

etc...
 
Share this answer
 
v4
Comments
Zia Ullah Khan 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 19-Dec-12 14:07pm    
Are you then trying to sort by the combined GPA?
Zia Ullah Khan 19-Dec-12 14:28pm    
no i'm not calculating the gpa
BC @ CV 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++;
Zia Ullah Khan 19-Dec-12 14:38pm    
can you have a look here please;
http://www.codeproject.com/Answers/512557/howplustopluscalculateplusthepluspositionplusofplu#answer2
You could use the Linq OrderBy method:
C#
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();
    }
 
Share this answer
 
Comments
Zia Ullah Khan 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 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)
void Main()
{
List<student> list = new List<student>();
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.
 
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