Solved

List<> order alphabetically

Posted on 2009-07-16
2
240 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

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…
For those of you who don't follow the news, or just happen to live under rocks, Microsoft Research released a beta SDK (http://www.microsoft.com/en-us/download/details.aspx?id=27876) for the Xbox 360 Kinect. If you don't know what a Kinect is (http:…
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…

740 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