suraj
suraj

Reputation: 11

Lambda expression for sorting list of collection

My input is below xml file

<Employees>
  <Department Position="9">
   <Employee No="7" Status="True" />
   <Employee No="6" Status="True" />
   <Employee No="8" Status="True" />
</Department>
<Department Position="4">
  <Employee No="7" Status="True" />
  <Employee No="8" Status="True" />
  <Employee No="6" Status="True" />
</Department>
</Employees>

Out put should  be  sorted  by department position  and  employee  "No"


<Employees>
 <Department Position="4">
  <Employee No="6" Status="True" />
  <Employee No="7" Status="True" />
  <Employee No="8" Status="True" />
 </Department>
 <Department Position="9">
   <Employee No="6" Status="True" />
   <Employee No="7" Status="True" />
   <Employee No="8" Status="True" />
 </Department>  

I have added below code but it returns either "position" wise or "No" wise but not both.

var sortSignalList = new Dictionary<int, List<string>>();

sortSignalList.OrderBy(x => x.Position).OrderBy(x=>x.No).ToList();

Upvotes: 1

Views: 54

Answers (3)

basundhara singh
basundhara singh

Reputation: 26

List can also be Sorted ThenByDescending

var thenByDescResult =sortSignalList.OrderBy(s => s.Position).ThenByDescending(s => s.No);

Upvotes: 0

Mick
Mick

Reputation: 6864

Try....

sortSignalList.OrderBy(x => x.Position).ThenBy(x=>x.No).ToList()

Upvotes: 0

Xiaosu
Xiaosu

Reputation: 615

sortSignalList.OrderBy(x => x.Position).ThenBy(x=>x.No).ToList();

Upvotes: 4

Related Questions