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
Solved

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

Posted on 2014-12-22
7
106 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
Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

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.

 

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

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

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

This article introduced a TextBox that supports transparent background.   Introduction TextBox is the most widely used control component in GUI design. Most GUI controls do not support transparent background and more or less do not have the…
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…
This video shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…

856 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