SortedList
-
Hi All I have a list Proces with PNaam,PNr,PAankomst and PStatus. This is show'n in listbox1. ListBox2 is a list of Burst with lengte and soort. This is of each Proces. Processes can have more Bursts but one Burst can only have one Proces. I have next two listboxes beside each other. In listbox3 I wanted to show PNaam sorted by PAankomst. In Listbox4 I wanted to show PNr, PAankomst, lengte and soort of that one Proces. (The user has to select one PNaam in listbox3 when I read the properties of that PNaam) I've got a class ProcesKlasse with members PNaam,PNr,PAankomst and PStatus. I've got a BurstKlasse with lengte and two inherented subclasses IOBurst and CPUBurst with each a member soort. I don't really know where to put the code. Also I've tried to search on internet but all the sugestions doesn't match... In Form.cs I've tried [code] Proces.Sort(delegate(Proces p1, Proces p2) {return p1.PAankomst.CompareTo(p2.PAankomst);}); [/code] but it can't find the PAankomst (it was private in ProcesKlasse, but even when it's public, I can't reach it) Also I've tried this in ProcesKlasse [code] public List SortedList(List) { for (Proces.PAankomst // can't go further :s } [/code] Is there someone who can give me a hint? Thank you! Anneke
-
Hi All I have a list Proces with PNaam,PNr,PAankomst and PStatus. This is show'n in listbox1. ListBox2 is a list of Burst with lengte and soort. This is of each Proces. Processes can have more Bursts but one Burst can only have one Proces. I have next two listboxes beside each other. In listbox3 I wanted to show PNaam sorted by PAankomst. In Listbox4 I wanted to show PNr, PAankomst, lengte and soort of that one Proces. (The user has to select one PNaam in listbox3 when I read the properties of that PNaam) I've got a class ProcesKlasse with members PNaam,PNr,PAankomst and PStatus. I've got a BurstKlasse with lengte and two inherented subclasses IOBurst and CPUBurst with each a member soort. I don't really know where to put the code. Also I've tried to search on internet but all the sugestions doesn't match... In Form.cs I've tried [code] Proces.Sort(delegate(Proces p1, Proces p2) {return p1.PAankomst.CompareTo(p2.PAankomst);}); [/code] but it can't find the PAankomst (it was private in ProcesKlasse, but even when it's public, I can't reach it) Also I've tried this in ProcesKlasse [code] public List SortedList(List) { for (Proces.PAankomst // can't go further :s } [/code] Is there someone who can give me a hint? Thank you! Anneke
Hi Anneke, 1. There is some confusion I am afraid. It would help if you gave real names to the listboxes for starters. Furthermore it seems to me you have a ProcesKlasse type (a class), and a list of such items, that you call Proces. This is confusing, and you are not helping yourself doing it this way. The better approach would be to call the Proces thingies "Proces", and the list of Proces items something like ProcesLijst. The list could actually be a
List< Proces>
assuming you run .NET 2.0 or better; or else an ArrayList. 2. You could sort a ProcesLijst by calling its Sort method and feeding it an object that knows how to order two Proces items. That requires some class implementing the IComparer interface, which is quite easy to do. It would just hold one method, something like:public int Compare(Proces p1, Proces p2) {
return p1.Aankomst-p2.Aankomst; // only if Aankomst is an int
// if not, whatever code that returns negative, zero or positive as required
}BTW: the Proces arguments above are my Proces class, not your Proces list! And that is why I don't have any trouble getting to their Aankomst member. The above should put you on the right track. Groeten! :)
Luc Pattyn [Forum Guidelines] [My Articles]
This month's tips: - before you ask a question here, search CodeProject, then Google; - the quality and detail of your question reflects on the effectiveness of the help you are likely to get; - use PRE tags to preserve formatting when showing multi-line code snippets.
-
Hi Anneke, 1. There is some confusion I am afraid. It would help if you gave real names to the listboxes for starters. Furthermore it seems to me you have a ProcesKlasse type (a class), and a list of such items, that you call Proces. This is confusing, and you are not helping yourself doing it this way. The better approach would be to call the Proces thingies "Proces", and the list of Proces items something like ProcesLijst. The list could actually be a
List< Proces>
assuming you run .NET 2.0 or better; or else an ArrayList. 2. You could sort a ProcesLijst by calling its Sort method and feeding it an object that knows how to order two Proces items. That requires some class implementing the IComparer interface, which is quite easy to do. It would just hold one method, something like:public int Compare(Proces p1, Proces p2) {
return p1.Aankomst-p2.Aankomst; // only if Aankomst is an int
// if not, whatever code that returns negative, zero or positive as required
}BTW: the Proces arguments above are my Proces class, not your Proces list! And that is why I don't have any trouble getting to their Aankomst member. The above should put you on the right track. Groeten! :)
Luc Pattyn [Forum Guidelines] [My Articles]
This month's tips: - before you ask a question here, search CodeProject, then Google; - the quality and detail of your question reflects on the effectiveness of the help you are likely to get; - use PRE tags to preserve formatting when showing multi-line code snippets.
Hi It's not only 2 objects that I have to compare but a whole list. In this list there's a member PAankomst which I have to compare with all the other objects in the list, also with their PAankomst. I've read that IComparer is only for when we have to compare two objects. Now I don't really find a method to compare a member in a list with other members in the same list. I've tried with IComparable<> and then the method CompareTo but it wouldn't work. Any suggestions?
-
Hi It's not only 2 objects that I have to compare but a whole list. In this list there's a member PAankomst which I have to compare with all the other objects in the list, also with their PAankomst. I've read that IComparer is only for when we have to compare two objects. Now I don't really find a method to compare a member in a list with other members in the same list. I've tried with IComparable<> and then the method CompareTo but it wouldn't work. Any suggestions?
Hi Anneke, your original code tried to sort a list; that is where you need an object that implements IComparer so you can specify your specific sort order. The Sort method holds all the code to iterate through the collection as often as needed to get it sorted, you only have to tell it what your sort order is by offering an IComparer object. On the other hand, if you need to FIND an object in a collection, you have to do that yourself, probably using a loop (such as foreach); you could still use the IComparer object inside the loop, but you don't have to do it that way. :)
Luc Pattyn [Forum Guidelines] [My Articles]
This month's tips: - before you ask a question here, search CodeProject, then Google; - the quality and detail of your question reflects on the effectiveness of the help you are likely to get; - use PRE tags to preserve formatting when showing multi-line code snippets.