Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. SortList problem

SortList problem

Scheduled Pinned Locked Moved C#
questiondatabasealgorithmshelptutorial
3 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • B Offline
    B Offline
    bonzaiholding
    wrote on last edited by
    #1

    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 < StudentNumber; i++)
            {
                Student std = GetThisStudent(i);
                sortList.Add(i, std);
                if (sortList.Count > 10)
                    sortList.RemoveAt(10);
            }
            
            return sortList;            
        }
    

    Thanks and regards, Shai, Bonzai.

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

    N 1 Reply Last reply
    0
    • B bonzaiholding

      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 < StudentNumber; i++)
              {
                  Student std = GetThisStudent(i);
                  sortList.Add(i, std);
                  if (sortList.Count > 10)
                      sortList.RemoveAt(10);
              }
              
              return sortList;            
          }
      

      Thanks and regards, Shai, Bonzai.

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

      N Offline
      N Offline
      N a v a n e e t h
      wrote on last edited by
      #2

      bonzaiholding wrote:

      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.

      You need insertion sort and don't sort the whole list for each insertion. When adding an item, compare with all the items in the list and find a suitable position for that new item. Complexity of this will be O(n).

      bonzaiholding wrote:

      (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?

      I don't see any problem here.

      bonzaiholding wrote:

      If i have SortList and i want to save 10 object with the best result how can i do it?

      Take the last or first 10 elements from the list depending on your sort order. :)

      Navaneeth How to use google | Ask smart questions

      B 1 Reply Last reply
      0
      • N N a v a n e e t h

        bonzaiholding wrote:

        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.

        You need insertion sort and don't sort the whole list for each insertion. When adding an item, compare with all the items in the list and find a suitable position for that new item. Complexity of this will be O(n).

        bonzaiholding wrote:

        (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?

        I don't see any problem here.

        bonzaiholding wrote:

        If i have SortList and i want to save 10 object with the best result how can i do it?

        Take the last or first 10 elements from the list depending on your sort order. :)

        Navaneeth How to use google | Ask smart questions

        B Offline
        B Offline
        bonzaiholding
        wrote on last edited by
        #3

        Thanks. I tryed some other methods but in the end i just use this function :

        private void InsertThisStudent(Student std, List<Student> List)
        {

                for (int i = 0; i < List.Count; i++)
                {
                    if (std.CompareTo(List\[i\]) < 0)
                    {
                        List.Insert(i, std);
                        return;
                    }
                }
                //If this is the last one
                List.Add(std);
            }
        

        thanks :-)

        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups