Reputation: 371
I have several lists with elements of same type, for instance List
class ClassSample
{
int param1;
int param2;
public ClassSample(int par1, int par2) {
param1 = par1;
param2 = par2;
}
}
List<ClassSample> list1 = new List<ClassSample>();
List<ClassSample> list2 = new List<ClassSample>();
List<ClassSample> list3 = new List<ClassSample>();
list1.Add(new ClassSample(2, 3));
list1.Add(new ClassSample(4, 2));
list2.Add(new ClassSample(3, 4));
list2.Add(new ClassSample(4, 6));
list2.Add(new ClassSample(2, 2));
list2.Add(new ClassSample(5, 3));
list3.Add(new ClassSample(4, 2));
list3.Add(new ClassSample(7, 3));
list3.Add(new ClassSample(4, 4));
For the ClassSample class I want to define different functions to get a ranking of an object having that type. For instance
long GetRankByProduct(ClassSample obj) {
return obj.param1 * obj.param2;
}
long GetRankBySum(ClassSample obj) {
return obj.param1 + obj.param2;
}
and so on.
I would like to sort the 3 lists by using different dynamical criteria, in form of functions that take as parameters a list of ClassSample objects and a ranking function for ClassSample.
// pseudo-code
public long GetListRankByMax(List<ClassSample> csList, Function<ClassSample, long> rankFunct) {
long max = csList.Max(x => rankFunct(x));
return max;
}
public long GetListRankByMin(List<ClassSample> csList, Function<ClassSample, long> rankFunct) {
long min = csList.Min(x => rankFunct(x));
return min;
}
public long GetListRankBySum(List<ClassSample> csList, Function<ClassSample, long> rankFunct) {
long sum = 0;
foreach (var cs in csList)
sum += rankFunct(cs);
return sum;
}
How would a code look like, where the 3 lists are sorted dynamically with different criteria, by passing dynamically the function for the "list ranking" and give it as parameter the function that calculates the "element ranking" ? (for instance to sort the list with the "ByMax" function and by applying to each list element the "ByProduct" ranking function, or whatever other combination).
I actually want to sort the 3 lists (and not the list elements), by using different criteria. For instance if the ClassSample list is inside a container class :
class ContainerClassSample {
List<ClassSample> ClsList {get;set;}
}
List<ContainerClassSample> containerList = new List<ContainerClassSample>();
ContainerClassSample container1 = new ContainerClassSample { ClsList = list1 };
ContainerClassSample container2 = new ContainerClassSample { ClsList = list2 };
ContainerClassSample container3 = new ContainerClassSample { ClsList = list3 };
containerList.Add(container1);
containerList.Add(container2);
containerList.Add(container3);
I need something like this (I should be able to change dynamically the two functions):
// pseudo-code
var result = containerList.OrderBy(GetListRankByMax, GetRankByProduct); // ??
Upvotes: 1
Views: 72
Reputation: 45096
why are you making this so hard
class ClassSample
{
int param1;
int param2;
public int Prod { get { return param1 * param1; } }
public int Sum { get { return param1 * param1; } }
public ClassSample(int par1, int par2) {
param1 = par1;
param2 = par2;
}
}
var sortedList = list2.OrderBy(x => x.Sum);
var MinProd = (from x in list2 select x.Prod).Min();
You can pass an IComparer to a Sort
You class can implement IComparable in a class
You are tying to reinvent stuff the framework already does
Upvotes: 1