惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

酷 壳 – CoolShell
酷 壳 – CoolShell
H
Hacker News: Front Page
P
Palo Alto Networks Blog
T
ThreatConnect
Apple Machine Learning Research
Apple Machine Learning Research
博客园_首页
T
True Tiger Recordings
P
Privacy & Cybersecurity Law Blog
B
Blog
IT之家
IT之家
Last Week in AI
Last Week in AI
F
Full Disclosure
Hacker News: Ask HN
Hacker News: Ask HN
C
Comments on: Blog
Microsoft Azure Blog
Microsoft Azure Blog
C
Cybersecurity and Infrastructure Security Agency CISA
Microsoft Security Blog
Microsoft Security Blog
博客园 - 【当耐特】
N
News and Events Feed by Topic
NISL@THU
NISL@THU
腾讯CDC
雷峰网
雷峰网
Security Latest
Security Latest
李成银的技术随笔
M
Microsoft Research Blog - Microsoft Research
L
LangChain Blog
L
Lohrmann on Cybersecurity
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
C
Check Point Blog
Y
Y Combinator Blog
Recent Announcements
Recent Announcements
博客园 - Franky
N
News | PayPal Newsroom
V
V2EX
A
About on SuperTechFans
The Register - Security
The Register - Security
月光博客
月光博客
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
Google Online Security Blog
Google Online Security Blog
MyScale Blog
MyScale Blog
Cisco Talos Blog
Cisco Talos Blog
Vercel News
Vercel News
WordPress大学
WordPress大学
C
Cyber Attacks, Cyber Crime and Cyber Security
The Hacker News
The Hacker News
IntelliJ IDEA : IntelliJ IDEA – the Leading IDE for Professional Development in Java and Kotlin | The JetBrains Blog
IntelliJ IDEA : IntelliJ IDEA – the Leading IDE for Professional Development in Java and Kotlin | The JetBrains Blog
爱范儿
爱范儿
A
Arctic Wolf
L
LINUX DO - 最新话题
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More

博客园 - answer

SQL查询重复记录 如何使用两台 NETGEAR 无线路由器进行无线中继(WDS) [转]取当前日期是在一年中的第几周 [转]写在UserControl销毁之时 - answer [转]检查本地DNS服务器是否正常工作及解决方法 SQLITE入门至精通 [转]WM手机,关于如何让手机一直运行下去,而不进入待机 - answer [转]string表达式运算 [转]HTC G11 ROOT获取权限教程 [转]WebForm 与 winform 路径获取 [转]win7 系统装SQLServer2000 成功。 Mobile Development: Disable Windows Mobile 6.5 Start and Close Button - answer [转]Windows Mobile: Hide StartButton in WinMo 6.5.x Windows CE 电源管理(转贴) [转]分享8个超棒的免费高质量图标搜索引擎 [转]Win7系统下VS2005_2008不识别WinCE5 SDK [转]windows 7 下ASP.net 本地配置 ( IIS 7) - answer [转]SQLite存取二进制数据 [转]SelectObject() 装载字体 VC EVC
给List排序( list sort)
answer · 2012-10-14 · via 博客园 - answer

Posted on 2012-10-14 23:41  answer  阅读(296)  评论(0)    收藏  举报

你有一个list,格式如list<userType>, 但是这个userType并没有继承自ICompare或者IComparable 等,那如何给这个list排序那?

指定一个类 这个类要继承自ICompare<userType>

public class SortUserGroup:IComparer<UserGroup>
    
{
        
/// <summary>
        
/// Sort by AccountID and UserGroupName
        
/// </summary>
        
/// <param name="userGroup1"></param>
        
/// <param name="userGroup2"></param>
        
/// <returns></returns>

        int IComparer<UserGroupEntityAccess>.Compare(UserGroup userGroup1, UserGroup userGroup2)
        
{
            
if (userGroup1 != null && userGroup2 != null)
            
{
                
if (userGroup1.AccountID == userGroup2.AccountID)
                    
return userGroup1.UserGroupName.CompareTo(userGroup2.UserGroupName);
                
else
                    
return userGroup1.AccountID > userGroup2.AccountID ? 1 : -1;

            }

            
return -1;
        }

    }

然后:

SortUserGroup sortGroup = new SortUserGroup();
List<UserGroup>.Sort(sortUserGroup)

这里是按照UserGroup的AccountID 和 UserGroupName 两个字段排序。