Solved

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

Posted on 2014-12-22
7
111 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
[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
7 Comments
 
LVL 34

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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

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

Enroll in June's Course of the Month

June's Course of the Month is now available! Every 10 seconds, a consumer gets hit with ransomware. Refresh your knowledge of ransomware best practices by enrolling in this month's complimentary course for Premium Members, Team Accounts, and Qualified Experts.

Question has a verified solution.

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

Article by: Najam
Having new technologies does not mean they will completely replace old components.  Recently I had to create WCF that will be called by VB6 component.  Here I will describe what steps one should follow while doing so, please feel free to post any qu…
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
There are cases when e.g. an IT administrator wants to have full access and view into selected mailboxes on Exchange server, directly from his own email account in Outlook or Outlook Web Access. This proves useful when for example administrator want…
In this video, viewers are given an introduction to using the Windows 10 Snipping Tool, how to quickly locate it when it's needed and also how make it always available with a single click of a mouse button, by pinning it to the Desktop Task Bar. Int…

707 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