Solved

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

Posted on 2014-12-22
7
100 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 33

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 75

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
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.

 

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 75

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 75

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

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

Suggested Solutions

Title # Comments Views Activity
Iterate two lists and remove inactive 11 49
Need a complete list of ASP.NET page load events 3 25
In WPF / C# binding a local database in code behind 1 24
Syntax Error 2 42
Introduction Although it is an old technology, serial ports are still being used by many hardware manufacturers. If you develop applications in C#, Microsoft .NET framework has SerialPort class to communicate with the serial ports.  I needed to…
This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.
Concerto provides fully managed cloud services and the expertise to provide an easy and reliable route to the cloud. Our best-in-class solutions help you address the toughest IT challenges, find new efficiencies and deliver the best application expe…

932 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