Solved

List<> order alphabetically

Posted on 2009-07-16
2
241 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

Salesforce Made Easy to Use

On-screen guidance at the moment of need enables you & your employees to focus on the core, you can now boost your adoption rates swiftly and simply with one easy tool.

Question has a verified solution.

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

A basic question.. “What is the Garbage Collector?” The usual answer given back: “Garbage collector is a background thread run by the CLR for freeing up the memory space used by the objects which are no longer used by the program.” I wondered …
Welcome my friends to the second instalment and follow-up to our Minify and Concatenate Your Scripts and Stylesheets (http://www.experts-exchange.com/Programming/Languages/.NET/ASP.NET/A_4334-Minify-and-Concatenate-Your-Scripts-and-Stylesheets.html)…
Finding and deleting duplicate (picture) files can be a time consuming task. My wife and I, our three kids and their families all share one dilemma: Managing our pictures. Between desktops, laptops, phones, tablets, and cameras; over the last decade…

751 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