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. Sorting a collection of classes [modified]

Sorting a collection of classes [modified]

Scheduled Pinned Locked Moved C#
algorithmstutorialhelplearning
4 Posts 4 Posters 1 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.
  • H Offline
    H Offline
    hpjchobbes
    wrote on last edited by
    #1

    Please note, I am still learning! I have a class that holds information about an object. I have another class that I would like to manage a collection of those objects. What I am trying to do is when a user updates some values for an object, that the manager then resorts the order of the collection automatically. I would also like to include an ability to 'lock' the sorting so a user could do a batch of changes and then sort at the end. I was looking at delegates, but I am not sure they are what I need. In fact, I am not even sure that what I want to do is really possible. Here is a basic example of what I am trying to do:

    class cThing
    {
    private float _depth;

    public float Depth
    {
    set
    {
    _depth = value;
    // Here is where I would tell my manager to resort the list
    }
    }
    }

    class cThingManager
    {
    public List MyThings = new List();

    public void Sort()
    {
    MyThings.Sort();
    }

    }

    I would imagine this is possible, but I don't know what it would be called so I've been having a hard time searching for information on this. Any help is appreciated! Edit - I figured out how to do the locking! -- modified at 22:50 Thursday 3rd May, 2007

    S J A 3 Replies Last reply
    0
    • H hpjchobbes

      Please note, I am still learning! I have a class that holds information about an object. I have another class that I would like to manage a collection of those objects. What I am trying to do is when a user updates some values for an object, that the manager then resorts the order of the collection automatically. I would also like to include an ability to 'lock' the sorting so a user could do a batch of changes and then sort at the end. I was looking at delegates, but I am not sure they are what I need. In fact, I am not even sure that what I want to do is really possible. Here is a basic example of what I am trying to do:

      class cThing
      {
      private float _depth;

      public float Depth
      {
      set
      {
      _depth = value;
      // Here is where I would tell my manager to resort the list
      }
      }
      }

      class cThingManager
      {
      public List MyThings = new List();

      public void Sort()
      {
      MyThings.Sort();
      }

      }

      I would imagine this is possible, but I don't know what it would be called so I've been having a hard time searching for information on this. Any help is appreciated! Edit - I figured out how to do the locking! -- modified at 22:50 Thursday 3rd May, 2007

      S Offline
      S Offline
      Stefan Troschuetz
      wrote on last edited by
      #2

      You could add an event to your cThing class (maybe called DepthChanged) that is fired whenever the depth changes. You're manager has to subscribe an event handler to the event of each item it manages and inside the event handler sort the list, if sorting is not locked.


      "Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning." - Rick Cook

      www.troschuetz.de

      1 Reply Last reply
      0
      • H hpjchobbes

        Please note, I am still learning! I have a class that holds information about an object. I have another class that I would like to manage a collection of those objects. What I am trying to do is when a user updates some values for an object, that the manager then resorts the order of the collection automatically. I would also like to include an ability to 'lock' the sorting so a user could do a batch of changes and then sort at the end. I was looking at delegates, but I am not sure they are what I need. In fact, I am not even sure that what I want to do is really possible. Here is a basic example of what I am trying to do:

        class cThing
        {
        private float _depth;

        public float Depth
        {
        set
        {
        _depth = value;
        // Here is where I would tell my manager to resort the list
        }
        }
        }

        class cThingManager
        {
        public List MyThings = new List();

        public void Sort()
        {
        MyThings.Sort();
        }

        }

        I would imagine this is possible, but I don't know what it would be called so I've been having a hard time searching for information on this. Any help is appreciated! Edit - I figured out how to do the locking! -- modified at 22:50 Thursday 3rd May, 2007

        J Offline
        J Offline
        Jon Hulatt
        wrote on last edited by
        #3

        are you asking how to sort on a custom object? or how to have a child class call a method on a parent class?

        using System.Beer;

        1 Reply Last reply
        0
        • H hpjchobbes

          Please note, I am still learning! I have a class that holds information about an object. I have another class that I would like to manage a collection of those objects. What I am trying to do is when a user updates some values for an object, that the manager then resorts the order of the collection automatically. I would also like to include an ability to 'lock' the sorting so a user could do a batch of changes and then sort at the end. I was looking at delegates, but I am not sure they are what I need. In fact, I am not even sure that what I want to do is really possible. Here is a basic example of what I am trying to do:

          class cThing
          {
          private float _depth;

          public float Depth
          {
          set
          {
          _depth = value;
          // Here is where I would tell my manager to resort the list
          }
          }
          }

          class cThingManager
          {
          public List MyThings = new List();

          public void Sort()
          {
          MyThings.Sort();
          }

          }

          I would imagine this is possible, but I don't know what it would be called so I've been having a hard time searching for information on this. Any help is appreciated! Edit - I figured out how to do the locking! -- modified at 22:50 Thursday 3rd May, 2007

          A Offline
          A Offline
          AFSEKI
          wrote on last edited by
          #4
          1. Your "class to be sorted" should implement IComparable interface with you T = your class name. 2) You should add an event handler to fire property change events of your "class to be sorted" objects 3) Catch these events in your collection and use Array.Sort(myCollection) OR 1) you can use a SortedList or SortedDisctionary as the collection of your "class to be sorted" objects and set TKey to the property of your "class to be sorted" but this property should be IComparable! 2) then when this property changes, your collection is automatically sorted BUT these 2 options fails in your need to do a batch update and then sort + the ability to lock sorting, SO: 1) You should use a BatchUpdate after a number of updates are done for any "class to be sorted" object by keeping a int _updatecount static property in your Collection which is increased by each update to any of your object( if you use my 1st way, then you can increase this value in the property change event handler in your collection ) OR 2) The easiest and best way, do whatever update you want, then call Array.Sort(myCollection) where myCollection is ICollection ;) Hope this helps...
          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