Reputation: 6403
I have 3 levels for a PlanSolution 1,2,3 and i need to sort by .levelId initially, and then alphabetically sort the solutions belonging to that level by .name
PlanSolution[] planSolutions = wsAccess.GetPlanSolutionsForPlanRisk(planRisk.Id);
List<PlanRisk> planSolutionsList = new List<PlanRisk>(planSolutions);
planSolutionsList.Sort(delegate(PlanSolution x, PlanSolution y)
{
//HELP lol
});
Upvotes: 0
Views: 362
Reputation: 91299
planSolutionsList.Sort(delegate(PlanSolution x, PlanSolution y) {
if (x.Level == x.Level) { // same level, compare by name
return x.Name.CompareTo(y.Name);
} else { // different level, compare by level
return x.Level.CompareTo(y.Level);
}
});
Upvotes: 0
Reputation: 91442
LINQ to the rescue!
planSolutionsList = planSolutions
.OrderBy(x => x.id)
.ThenBy(x => x.Name)
.ToList(); // is this necessary?
For the original format -- you just want your comparer to prioritize ID over name
planSolutionsList.Sort( (x, y) =>
{
//HELP lol
if (x.id != y.id) return x.id - y.id;
else return x.Name.CompareTo(y.Name);
});
Upvotes: 6
Reputation: 10482
you could use Linq
planSolutionsList.OrderBy(x => x.Level).ThenBy(x => x.Name).ToList();
Upvotes: 1
Reputation:
For a language-acnostic approach: Do each one in order.
1) Sort on the level. 2) Remember the starting locations of each level after sorting 3) repeat for next sorting crieteria, only sorting the sub-sections.
Upvotes: 0