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. Sorted collection

Sorted collection

Scheduled Pinned Locked Moved C#
csharphelpquestion
7 Posts 5 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.
  • C Offline
    C Offline
    Chiman1
    wrote on last edited by
    #1

    I am working on an application where I have created a class emp which has ID and Name as a property. Now I want to create a collection of Emp class , which should be sorted based on the either ID or Name. (Program will decide whether it want to data sorted based on the Name or Id at the runtime). For and employ ID and NAMe property is not unique. It is possible that employed is having similar name. Can you please help me to decide which .net collection object should be used to achieve above functionality? If possible please provide a snippet. public class Emp { private int myID; public int ID { get { return myID; } set { myID = value; } } private string myName; public string Name { get { return myName; } set { myName = value; } } }

    A L R 3 Replies Last reply
    0
    • C Chiman1

      I am working on an application where I have created a class emp which has ID and Name as a property. Now I want to create a collection of Emp class , which should be sorted based on the either ID or Name. (Program will decide whether it want to data sorted based on the Name or Id at the runtime). For and employ ID and NAMe property is not unique. It is possible that employed is having similar name. Can you please help me to decide which .net collection object should be used to achieve above functionality? If possible please provide a snippet. public class Emp { private int myID; public int ID { get { return myID; } set { myID = value; } } private string myName; public string Name { get { return myName; } set { myName = value; } } }

      A Offline
      A Offline
      Abhinav S
      wrote on last edited by
      #2

      Have a look at the SortedList.

      My signature "sucks" today

      C 1 Reply Last reply
      0
      • C Chiman1

        I am working on an application where I have created a class emp which has ID and Name as a property. Now I want to create a collection of Emp class , which should be sorted based on the either ID or Name. (Program will decide whether it want to data sorted based on the Name or Id at the runtime). For and employ ID and NAMe property is not unique. It is possible that employed is having similar name. Can you please help me to decide which .net collection object should be used to achieve above functionality? If possible please provide a snippet. public class Emp { private int myID; public int ID { get { return myID; } set { myID = value; } } private string myName; public string Name { get { return myName; } set { myName = value; } } }

        L Offline
        L Offline
        Luc Pattyn
        wrote on last edited by
        #3

        There are two ways to go: - you could use a collection that sorts itself everytime you add/remove an item; those are pretty good if the sorting criterium is constant. - you could use any collection that fits your app, then sort it if and when you need it sorted; you can specify the sort criterium dynamically. This article[^] deals with the subject. :)

        Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles]


        I only read formatted code with indentation, so please use PRE tags for code snippets.


        I'm not participating in frackin' Q&A, so if you want my opinion, ask away in a real forum (or on my profile page).


        M 1 Reply Last reply
        0
        • A Abhinav S

          Have a look at the SortedList.

          My signature "sucks" today

          C Offline
          C Offline
          Chiman1
          wrote on last edited by
          #4

          SortList require a unique key value and in our case name can be duplicate.

          1 Reply Last reply
          0
          • C Chiman1

            I am working on an application where I have created a class emp which has ID and Name as a property. Now I want to create a collection of Emp class , which should be sorted based on the either ID or Name. (Program will decide whether it want to data sorted based on the Name or Id at the runtime). For and employ ID and NAMe property is not unique. It is possible that employed is having similar name. Can you please help me to decide which .net collection object should be used to achieve above functionality? If possible please provide a snippet. public class Emp { private int myID; public int ID { get { return myID; } set { myID = value; } } private string myName; public string Name { get { return myName; } set { myName = value; } } }

            R Offline
            R Offline
            riced
            wrote on last edited by
            #5

            Possible solution.

            public class Emp
            {
            private int myID;
            public int ID
            {
            get { return myID; }
            set { myID = value; }
            }

            private string myName;
            public string Name
            {
            get { return myName; }
            set { myName = value; }
            }

            public static List<Emp> GetEmployees()
            {
            //return a list of emp objects
            }
            }

            In calling code use:

            //Using var allows type returned by GetEmployees to be
            //any thing that implements IEnumerable<T>
            var emps = Emp.GetEmployees(); //Fill the list
            foreach (Emp emp in emps.OrderBy(e => e.Name))
            {
            Console.WriteLine(emp);
            }

            foreach (Emp emp in emps.OrderBy(e => e.ID))
            {
            Console.WriteLine(emp);
            }

            Regards David R --------------------------------------------------------------- "Every program eventually becomes rococo, and then rubble." - Alan Perlis The only valid measurement of code quality: WTFs/minute.

            1 Reply Last reply
            0
            • L Luc Pattyn

              There are two ways to go: - you could use a collection that sorts itself everytime you add/remove an item; those are pretty good if the sorting criterium is constant. - you could use any collection that fits your app, then sort it if and when you need it sorted; you can specify the sort criterium dynamically. This article[^] deals with the subject. :)

              Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles]


              I only read formatted code with indentation, so please use PRE tags for code snippets.


              I'm not participating in frackin' Q&A, so if you want my opinion, ask away in a real forum (or on my profile page).


              M Offline
              M Offline
              mabo42
              wrote on last edited by
              #6

              I guess you meant: -you could use a collection that sorts itself everytime you add/remove an item; :)

              L 1 Reply Last reply
              0
              • M mabo42

                I guess you meant: -you could use a collection that sorts itself everytime you add/remove an item; :)

                L Offline
                L Offline
                Luc Pattyn
                wrote on last edited by
                #7

                yes, that would be good. :)

                Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles]


                I only read formatted code with indentation, so please use PRE tags for code snippets.


                I'm not participating in frackin' Q&A, so if you want my opinion, ask away in a real forum (or on my profile page).


                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