I have a list of objects, I want to sort this list of object depending of a certain order ( that will be dynamically determined)
For the moment I was Able to sort it (Hardcoded)
public void DynamicalSort(List<ProduitEdi> lProd,List<Affichages> Affichages)
{
var ordredAffichages = Affichages.Where(x => x.IsActive).OrderBy(y => y.Ordre)
.ToList();
// Will return a list with string [Year,Price,Family,Brand]
lProd.Sort((prod1, prod2) =>
{
int compare = String.Compare(prod1.Family, prod2.Family, StringComparison.Ordinal);
if (compare != 0) return compare;
compare = String.Compare(prod1.Brand, prod2.Brand, StringComparison.Ordinal);
if (compare != 0) return compare;
compare = prod1.Price != null ? String.Compare(prod1.Price, prod2.Price, StringComparison.Ordinal) : 1;
if (compare != 0) return compare;
compare = prod1.Year != null ? String.Compare(prod1.Year, prod2.Year, StringComparison.Ordinal) : 1;
return compare;
});
}
In this result I was able to sort it Family first , Brand, Price , and Year
But now my list will be dynamically created and sorted, and I want the sort to follow the order of the list OrdredAffichages that will contain list of string.