المساعد الشخصي الرقمي

مشاهدة النسخة كاملة : SortList problem



C# Programming
08-19-2009, 01:50 PM
Until now i used a "List" to going over a lot of students (not from DB) and i want to keep to best 10 student (maybe later i will want to save 10,000 and more) .
I used to list and every time im insert a student and then sort the list. After the sorting im checking if there is more then 10 student and then take out the lower grade in the list.
The problem is that every iteration im sorting the list that is allready sorted because there is one new stduent that been insert to the list.
Is there ant other way to do it?

(i tryed to use SortList and you can see the problem , the key is the grade and it's possible that 2 student will have the same grade).
What is the best option?



If i have SortList and i want to save 10 object with the best result how can i do it?
For example if i have a student class and the comparison function between 2 stduent is by grade how can i save a list with the best 10 stduent?
(if 2 stduent have the same grade i have a problem with the key).



class Student : IComparable
{
public int id;
public int grade;
public string studentName;

//IComparable Members
public int CompareTo(object obj)
{
Student std = (Student)obj;
return std.grade - this.grade;
}

}


//return the best 10 stduent
public SortedList GetTheBest10Student()
{
SortedList sortList = new SortedList();

for (int i = 0; i </span StudentNumber; i++)br / {br / Student std = GetThisStudent(i);br / sortList.Add(i, std);br / span class="code-keyword"if/span (sortList.Count span class="code-keyword"> 10)
sortList.RemoveAt(10);
}

return sortList;
}



Thanks and regards,
Shai, Bonzai.

modified on Wednesday, August 19, 2009 5:12 AM