Got stuck trying to sort my List<>
in C#.
I have an interface type with its implementation class:
public interface IRoom
{
// ...
}
public class Room : IRoom
{
// ...
}
And a base class for my comparers:
public abstract class RoomComparer : IComparer<IRoom>
{
public RoomComparer()
{
}
public abstract int Compare(IRoom x, IRoom y);
}
And two its children:
public class StandardMultipliedSquareMetersComparer : RoomComparer
{
public StandardMultipliedSquareMetersComparer()
{
}
public override int Compare(IRoom a, IRoom b)
{
// ...
}
}
public class SquareMetersComparer : RoomComparer
{
public SquareMetersComparer()
{
}
public override int Compare(IRoom a, IRoom b)
{
// ...
}
}
Now, here's where the problems begin: I got a generic class Hotel
with a list of my Room
instances:
public class Hotel<TRoom, TComparer> : IHotel, IEnumerable<TRoom>
where TRoom : IRoom
where TComparer : RoomComparer, new()
{
public List<TRoom> Rooms;
protected TComparer Comparer;
public Hotel(TRoom[] rooms)
{
Rooms = new List<TRoom>();
Rooms.AddRange(rooms);
Comparer = new TComparer();
Rooms.Sort(Comparer);
}
}
And here's the trouble - I got two errors on the line Rooms.Sort(Comparer);
:
Error CS1502: The best overloaded method match for `System.Collections.Generic.List.Sort(System.Collections.Generic.IComparer)' has some invalid arguments (CS1502)
Error CS1503: Argument
#1' cannot convert
TComparer' expression to type `System.Collections.Generic.IComparer' (CS1503)
I tried many different solutions, but no result. What's happening?
Note: using Mono 3.10.0
on Ubuntu