Subscribe to Subscribers
Talk to Webmaster Tony John


Forums » .NET » .NET »

Sort generic list list by two paramaeter


Posted Date: 08 Aug 2012      Posted By:: Sagar S pawar     Member Level: Gold    Member Rank: 259     Points: 1   Responses: 3



I want to sort generic list first by name desc and then by age asc.



Regards
Sagar pawar
Software developer




Responses

#683539    Author: jogesh      Member Level: Gold      Member Rank: 229     Date: 08/Aug/2012   Rating: 2 out of 52 out of 5     Points: 2

Try this

yourListName.Sort("Name",true);

yourListName.Sort("Age",false);

Hope this may help you...



 
#683551    Author: Anil Kumar Pandey      Member Level: Platinum      Member Rank: 1     Date: 08/Aug/2012   Rating: 2 out of 52 out of 5     Points: 2

You can try using LINQ as the below sample code describe how.


List<SomeClass>() a;
List<SomeClass> b = a.OrderBy(x => x.x).ThenBy(x => x.y).ToList();



Thanks & Regards
Anil Kumar Pandey
Microsoft MVP, DNS MVM



 
#683575    Author: Asheej T K        Member Level: Diamond      Member Rank: 2     Date: 08/Aug/2012   Rating: 2 out of 52 out of 5     Points: 4

Hi,
Please check below code,


public class Employee : IComparable<Employee>
{
private int id;
private string EmpName;
private Int Age;

public static Comparison<Employee> AgeComparison = delegate(Employee e1, Employee e2)
{
return e1.Age.CompareTo(e2.Age);
};

public static Comparison<Employee> IDComparison = delegate(Employee e1, Employee e2)
{
return e1.id.CompareTo(e2.id);
};

public int EmployeeID
{
get { return id; }
set { id = value; }
}

public string EmployeeName
{
get { return EmpName; }
set { EmpName = value; }
}

public Int UnitAge
{
get { return Age; }
set { Age = value; }
}

public Employee(int id, string EmpName, Int Age)
{
this.id = id;
this.EmpName = EmpName;
this.Age = Age;
}


public int CompareTo(Employee other)
{
return EmployeeName.CompareTo(other.EmployeeName);
}


public override string ToString()
{
return string.Format("Id: {0} Name: {1} Age: {2}", id, EmpName, Age);
}
}



Regards,
Asheej T K
Microsoft MVP[ASP.NET/IIS]
DotNetSpider MVM

Dotnet Galaxy






 
Post Reply
You must Sign In to post a response.

Next : javascript not calling for first time in gridrowedit click
Previous : How to add the .dll in project?
Return to Discussion Forum
Post New Message
Category:

Related Messages



Follow us on Twitter: https://twitter.com/dotnetspider

Active Members
TodayLast 7 Daysmore...

Awards & Gifts
Copyright © SpiderWorks Technologies Pvt Ltd., Kochi, India
2005 - 2012 All Rights Reserved.
.NET and other trademarks mentioned in this site belong to Microsoft and other respective trademark owners.
Articles, tutorials and all other content offered here is for educational purpose only.
We are not associated with Microsoft or its partners.