-4

I have a generic list manager class ListManager which I use to store Animal objects in the AnimalManager subclass to the list manager.

In the Animal class I have added the IComparer interface and added a Compare method.

For some reason i get an error in the ListManager in the sort method se code below.

in ListManager....

//Method for sorting.
public void SortComp(T aType)
{
    aList.Sort(aType);  //This is where I get the error
}

in Animal....

//Def field sortby enum with default value name
private SortBy compareField = SortBy.Name;

//Getter and setter
public SortBy CompareField
{
     get { return compareField; }
     set
     {
        compareField = value;
     }
}

//Method (as specified by IComparer) for comparing two objects. Makes use of CompareTo method
public int Compare(Animal x, Animal y)
{
   //Depending on how the comparefield is set do different sorting 
    switch (compareField)
    {
        case SortBy.Name:
            return x.Name.CompareTo(y.Name);

         case SortBy.ID:
            return x.ID.CompareTo(y.ID);

         default:
            return x.Name.CompareTo(y.Name);//Just in case
    }
}
vc 74
  • 37,131
  • 7
  • 73
  • 89
Nungwe
  • 29
  • 6
  • 5
    "an error"? Please be specific which error you get. Also, what does "list.Sort(element)" *mean*? – Lasse V. Karlsen Mar 22 '18 at 08:29
  • 1
    Also, it is extremely uncommon and probably problematic/wrong to embed the sorting information into each element in a list, what if two elements specify different sorting information? – Lasse V. Karlsen Mar 22 '18 at 08:30
  • Please post the error - we're not mind readers. – Reinstate Monica Cellio Mar 22 '18 at 08:38
  • error is Argument 1: cannot convert from ´T´ to ´Syste.Collections.Generic.IComparer – Nungwe Mar 22 '18 at 08:43
  • I first created an animalComparer class bu that didnt work either...got the same error – Nungwe Mar 22 '18 at 08:44
  • so I agree with you Lasse.... – Nungwe Mar 22 '18 at 08:52
  • The `IComparable` interfaces are implemented by elements so that one element can compare itself to another, the `IComparer` interfaces are implemented externally so that something else can compare two elements. For the `Sort` method you need an `IComparer`, but it has to be implemented by the object, parameter, you give to the sort method, and not by the elements themselves. – Lasse V. Karlsen Mar 22 '18 at 09:31
  • Yes thats what i started out with i.e public void SortComp (AnimalComparer animalComparer.....) but I got the same error (AnimalComparer implements IComparer – Nungwe Mar 22 '18 at 10:12

2 Answers2

0

In the end a made my list protected (instead of private) accessed the list in the specifik listmanager (AnimalManager) with the AnimalComparer object as input to the Sort method. This worked but I would really liked to have done it in the generic listmanager instead leaving the list private.

Nungwe
  • 29
  • 6
0

Made some more research and then found an even better solution by using the IComparer interface directly in the the general manager.

public void Sort(IComparer sorter) {

aList.Sort(sorter);

}

where sorter is an object containing the corresponding sort methods.

Nungwe
  • 29
  • 6