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. [solved] Proper way to sort a List<> Custom objects

[solved] Proper way to sort a List<> Custom objects

Scheduled Pinned Locked Moved C#
data-structuresquestion
6 Posts 4 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.
  • D Offline
    D Offline
    Dwayner79
    wrote on last edited by
    #1

    I have been thinking about this for a while and want to be correct in how I go about this. I have a custom struct.

        struct MYSTRUCT
        {
            public double address;
            public short datLen;
            public string data;
        }
    

    I am using an array for these, and chose the List<> because of the Add method. What I want to do is resort the list based on the Address highest to lowest. I am pretty unfamiliar with the IComparer and Comparison option in the built in List<>Sort. Will one of those work for me? Do I override the Sort method? Do I just create a new method to sort the list? How would you guys go about this? Thanks in advance.

    ***************** "We need to apply 21st-century information technology to the health care field. We need to have our medical records put on the I.T." —GW

    modified on Sunday, August 30, 2009 10:14 PM

    H D L 4 Replies Last reply
    0
    • D Dwayner79

      I have been thinking about this for a while and want to be correct in how I go about this. I have a custom struct.

          struct MYSTRUCT
          {
              public double address;
              public short datLen;
              public string data;
          }
      

      I am using an array for these, and chose the List<> because of the Add method. What I want to do is resort the list based on the Address highest to lowest. I am pretty unfamiliar with the IComparer and Comparison option in the built in List<>Sort. Will one of those work for me? Do I override the Sort method? Do I just create a new method to sort the list? How would you guys go about this? Thanks in advance.

      ***************** "We need to apply 21st-century information technology to the health care field. We need to have our medical records put on the I.T." —GW

      modified on Sunday, August 30, 2009 10:14 PM

      H Offline
      H Offline
      Henry Minute
      wrote on last edited by
      #2

      For lots of examples of the various ways to do this, google c# sorting generic lists.

      Henry Minute Do not read medical books! You could die of a misprint. - Mark Twain Girl: (staring) "Why do you need an icy cucumber?" “I want to report a fraud. The government is lying to us all.”

      1 Reply Last reply
      0
      • D Dwayner79

        I have been thinking about this for a while and want to be correct in how I go about this. I have a custom struct.

            struct MYSTRUCT
            {
                public double address;
                public short datLen;
                public string data;
            }
        

        I am using an array for these, and chose the List<> because of the Add method. What I want to do is resort the list based on the Address highest to lowest. I am pretty unfamiliar with the IComparer and Comparison option in the built in List<>Sort. Will one of those work for me? Do I override the Sort method? Do I just create a new method to sort the list? How would you guys go about this? Thanks in advance.

        ***************** "We need to apply 21st-century information technology to the health care field. We need to have our medical records put on the I.T." —GW

        modified on Sunday, August 30, 2009 10:14 PM

        D Offline
        D Offline
        DaveyM69
        wrote on last edited by
        #3

        If you always want to sort on one field (i.e. address) then implement IComparable<T> It's really simple:

        struct MYSTRUCT : IComparable<MYSTRUCT>
        {
        public double address;
        public short datLen;
        public string data;

        public int CompareTo(MYSTRUCT other)
        {
            return address.CompareTo(other.address);
        }
        

        }

        Now just call Sort on your collection and it will automatically use your CompareTo method.

        Dave
        BTW, in software, hope and pray is not a viable strategy. (Luc Pattyn)
        Visual Basic is not used by normal people so we're not covering it here. (Uncyclopedia)
        Why are you using VB6? Do you hate yourself? (Christian Graus)

        1 Reply Last reply
        0
        • D Dwayner79

          I have been thinking about this for a while and want to be correct in how I go about this. I have a custom struct.

              struct MYSTRUCT
              {
                  public double address;
                  public short datLen;
                  public string data;
              }
          

          I am using an array for these, and chose the List<> because of the Add method. What I want to do is resort the list based on the Address highest to lowest. I am pretty unfamiliar with the IComparer and Comparison option in the built in List<>Sort. Will one of those work for me? Do I override the Sort method? Do I just create a new method to sort the list? How would you guys go about this? Thanks in advance.

          ***************** "We need to apply 21st-century information technology to the health care field. We need to have our medical records put on the I.T." —GW

          modified on Sunday, August 30, 2009 10:14 PM

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

          Hi, I think I covered that from several angles in this little article[^]. :)

          Luc Pattyn

          :badger: :jig: :badger:

          Have a look at my entry for the lean-and-mean competition; please provide comments, feedback, discussion, and don’t forget to vote for it! Thank you.

          :jig: :badger: :jig:

          1 Reply Last reply
          0
          • D Dwayner79

            I have been thinking about this for a while and want to be correct in how I go about this. I have a custom struct.

                struct MYSTRUCT
                {
                    public double address;
                    public short datLen;
                    public string data;
                }
            

            I am using an array for these, and chose the List<> because of the Add method. What I want to do is resort the list based on the Address highest to lowest. I am pretty unfamiliar with the IComparer and Comparison option in the built in List<>Sort. Will one of those work for me? Do I override the Sort method? Do I just create a new method to sort the list? How would you guys go about this? Thanks in advance.

            ***************** "We need to apply 21st-century information technology to the health care field. We need to have our medical records put on the I.T." —GW

            modified on Sunday, August 30, 2009 10:14 PM

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

            Are you sure address is a double? for home addresses, I'd expect a string. for enumerable addresses (as in memory addresses), I would want an integer (probably uint/int or ulong/long) which is exact and maybe can be used as an array index, not something that needs conversions from double to int all the time. :)

            Luc Pattyn

            :badger: :jig: :badger:

            Have a look at my entry for the lean-and-mean competition; please provide comments, feedback, discussion, and don’t forget to vote for it! Thank you.

            :jig: :badger: :jig:

            D 1 Reply Last reply
            0
            • L Luc Pattyn

              Are you sure address is a double? for home addresses, I'd expect a string. for enumerable addresses (as in memory addresses), I would want an integer (probably uint/int or ulong/long) which is exact and maybe can be used as an array index, not something that needs conversions from double to int all the time. :)

              Luc Pattyn

              :badger: :jig: :badger:

              Have a look at my entry for the lean-and-mean competition; please provide comments, feedback, discussion, and don’t forget to vote for it! Thank you.

              :jig: :badger: :jig:

              D Offline
              D Offline
              Dwayner79
              wrote on last edited by
              #6

              This is why I love codeproject. It should be a ulong. Thanks!

              ***************** "We need to apply 21st-century information technology to the health care field. We need to have our medical records put on the I.T." —GW

              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