Solved

List<> order alphabetically

Posted on 2009-07-16
2
237 Views
Last Modified: 2013-12-17
I want to order object in List<> alphabetically.
How do I order this list alphabetically according to FamilyName? The result should be (2,1,3)

List<User>uList=new List<User>;
User u1=new User;
u1.FirstName="John";
u1.FamilyName="Johnson";
uList.add(u1);
User u2=new User;
u2.FirstName="Howard";
u2.FamilyName="Anderson";
uList.add(u2);
User u3=new User;
u3.FirstName="Jim";
u3.FamilyName="Reagan";
uList.add(u3);
0
Comment
Question by:johnkainn
  • 2
2 Comments
 
LVL 53

Expert Comment

by:Dhaest
ID: 24868435
You can use linq to sort a list of objects

var orderedList = uList
           .OrderBy(x => x.FamilyName) .ToList();
0
 
LVL 53

Accepted Solution

by:
Dhaest earned 250 total points
ID: 24868450
Another possibility:

The following code helps in sorting list of objects based on some propertys value.
Source: http://cherupally.blogspot.com/2009/04/how-to-sort-list-of-objects-in-c.html

Console.WriteLine(Sorted list, by name);

uList.Sort(delegate(Person p1, Person p2) { return p1.FamilyName.CompareTo(p2.FamilyName); });

uList.ForEach(delegate(User p) { Console.WriteLine(String.Format({0} {1}, p.FamilyName, p.FirstName)); });

Open in new window

0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In my previous article (http://www.experts-exchange.com/Programming/Languages/.NET/.NET_Framework_3.x/A_4362-Serialization-in-NET-1.html) we saw the basics of serialization and how types/objects can be serialized to Binary format. In this blog we wi…
In my previous two articles we discussed Binary Serialization (http://www.experts-exchange.com/A_4362.html) and XML Serialization (http://www.experts-exchange.com/A_4425.html). In this article we will try to know more about SOAP (Simple Object Acces…
Migrating to Microsoft Office 365 is becoming increasingly popular for organizations both large and small. If you have made the leap to Microsoft’s cloud platform, you know that you will need to create a corporate email signature for your Office 365…
Both in life and business – not all partnerships are created equal. As the demand for cloud services increases, so do the number of self-proclaimed cloud partners. Asking the right questions up front in the partnership, will enable both parties …

863 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now