tags:

views:

110

answers:

4

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
                    });
+6  A: 

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);
});
Jimmy
A: 

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.

San Jacinto
+1  A: 

you could use Linq

planSolutionsList.OrderBy(x => x.Level).ThenBy(x => x.Name).ToList();
ghills
A: 
  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);
    }
  });
JG