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