Solved

How to built-in sort generic collection using one of the attribute?

Posted on 2014-12-22
7
98 Views
Last Modified: 2014-12-23
Hi, I'm using vs2012
I have a collection, List<UserPrincipal>.  How can I use the the buit-in collection Sort function to sort the collection based on one of the attribute of UserPrincipal (SamAccountName)?  Looked at MSDN but didn't see any example code on how to do this.

Thank you.
0
Comment
Question by:lapucca
7 Comments
 
LVL 32

Expert Comment

by:it_saige
ID: 40514116
This works for me:
using System;
using System.Collections.Generic;
using System.DirectoryServices.AccountManagement;
using System.Linq;

namespace EE_Q28585582
{
	class Program
	{
		private static List<UserPrincipal> users = new List<UserPrincipal>();

		static void Main(string[] args)
		{
			PrincipalContext context = new PrincipalContext(ContextType.Domain);
			UserPrincipal user = new UserPrincipal(context);
			PrincipalSearcher searcher = new PrincipalSearcher(user);
			foreach (Principal result in searcher.FindAll())
			{
				UserPrincipal upe = result as UserPrincipal;
				if (upe != null)
					users.Add(upe);
			}

			foreach (UserPrincipal up in users)
				Console.WriteLine(up.Name);
			Console.ReadLine();

			var usort = (from up in users orderby up.SamAccountName select up);
			foreach (UserPrincipal up in usort)
				Console.WriteLine(up.Name);
			Console.ReadLine();
		}
	}
}

Open in new window


-saige-
0
 
LVL 52

Accepted Solution

by:
Carl Tawn earned 500 total points
ID: 40514125
Try:
list.Sort(delegate(UserPrincipal a, UserPrincipal b) { return string.Compare(a.SAMAccountName, b.SAMAccountName); });

Open in new window

0
 
LVL 74

Expert Comment

by:käµfm³d 👽
ID: 40514146
*No Points*

Slightly shorter version of Carl's suggestion:

list.Sort((left, right) => string.Compare(left.SAMAccountName, right.SAMAccountName));

Open in new window

0
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 

Author Comment

by:lapucca
ID: 40514394
Carl,
That works great thank you.
Kaufmed, Your loc gives red underline.  Error message says can't convert because it's not delegate.
0
 
LVL 74

Expert Comment

by:käµfm³d 👽
ID: 40514407
It's possible you may need to include the type as well--usually you don't have to.

e.g.

list.Sort((UserPrincipal left, UserPrincipal right) => string.Compare(left.SAMAccountName, right.SAMAccountName));

Open in new window

0
 
LVL 74

Expert Comment

by:käµfm³d 👽
ID: 40514414
That's what I get for trying to do it from memory  ; )

Corrected:

s.Sort((left, right) => { return string.Compare(left.SamAccountName, right.SamAccountName); });

Open in new window

0
 

Author Comment

by:lapucca
ID: 40515378
Hi, I have a new question related to this one, ID: 28586081
Appreciate it if you can have a look.
0

Featured Post

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

Summary: Persistence is the capability of an application to store the state of objects and recover it when necessary. This article compares the two common types of serialization in aspects of data access, readability, and runtime cost. A ready-to…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

708 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

15 Experts available now in Live!

Get 1:1 Help Now