using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication5
{
class Program
{
static void Main(string[] args)
{
List<PermCategory> list = new List<PermCategory>();
list.Add(new PermCategory { CateId = 1, Name = "ad5", Perm = 1, Summary = "ada5" });
list.Add(new PermCategory { CateId=1,Name="ad",Perm=1,Summary="ada"});
list.Add(new PermCategory { CateId = 1, Name = "ad4", Perm = 1, Summary = "ada4" });
list.Add(new PermCategory { CateId = 2, Name = "a1d", Perm = 2, Summary = "a1da" });
list.Add(new PermCategory { CateId = 2, Name = "ad", Perm = 2, Summary = "ada" });
Console.WriteLine("排序前");
test(list);

list.Sort((x, y) => {
int result = 0;

// result -1小到大 ,  1大到小
#region A:perm 小到大,name 长度 小到大
//if (x.Perm < y.Perm)
//{
// result = -1;
//}
//if (x.Perm < y.Perm || x.Name.Length < y.Name.Length)
//{
// result = -1;
//}
#endregion
#region A:perm 小到大,name 长度 小到大
//if (x.Perm < y.Perm)
//{
// result = -1;
//}
//if (x.Perm == y.Perm && x.Name.Length < y.Name.Length)
//{
// result = -1;
//}
#endregion

#region A:perm 小到大,name 长度 大到小
if (x.Perm < y.Perm)
{
result = -1;
}
if (x.Perm == y.Perm && x.Name.Length > y.Name.Length)
{
result = -1;
}
#endregion
return result;
});
Console.WriteLine("---------------------");
test(list);
Console.WriteLine("排序后");
Console.ReadLine();

}
static void test(List<PermCategory> list) {
foreach (var item in list)
{
Console.WriteLine(string.Format("perm:{0},name:{1}",item.Perm,item.Name));
}
}
}
public class PermCategory //: IJsonlizable
{
// public PermCategory();

public int CateId { get; set; }

public string Name { get; set; }
public int Perm { get; set; }

public string Summary { get; set; }

// public string Jsonlize();
}
}

http://www.cnblogs.com/dj1232090/p/5844991.html

msdn List sort排序 IComparable 用法的更多相关文章

  1. List<T>.Sort() 排序的用法

    List<T> 可以通过 .Sort()进行排序,但是当 T 对象为自定义类型时(比如自定义模型),就需要 IComparable接口重写其中的方法来实现,实现代码如下: class Pr ...

  2. js数组之sort排序的用法

    sort排序 转载自:https://blog.csdn.net/idomyway/article/details/80544509 js中用方法sort()为数组排序.sort()方法有一个可选参数 ...

  3. C++ 中的sort()排序函数用法

    sort(first_pointer,first_pointer+n,cmp) 该函数可以给数组,或者链表list.向量排序. 实现原理:sort并不是简单的快速排序,它对普通的快速排序进行了优化,此 ...

  4. C#之IComparable用法,实现List<T>.sort()排序

    这篇文章主要介绍了C#的一些基础知识,主要是IComparable用法,实现List<T>.sort()排序,非常的实用,这里推荐给大家.   List<T>.sort()可以 ...

  5. JS基础篇--sort()方法的用法,参数以及排序原理

    JS基础篇--sort()方法的用法,参数以及排序原理   sort() 方法用于对数组的元素进行排序,并返回数组.默认排序顺序是根据字符串Unicode码点.语法:arrayObject.sort( ...

  6. js sort() 排序用法(转载)

    原文:https://blog.csdn.net/m0_37885651/article/details/80016718 sort() 方法用于对数组的元素进行排序,并返回数组.默认排序顺序是根据字 ...

  7. 反向输出及sort排序

    建立条件:#include "algorithm"引用这个头文件 1.reverse 的用法,反向排序,由自己输入5个数: 1 2 3 4 5 for (int i = 0; i ...

  8. javascript:算法之数组sort排序

    数组sort排序 sort比较次数,sort用法,sort常用 描述 方法sort()将在原数组上对数组元素进行排序,即排序时不创建新的数组副本.如果调用方法sort()时没有使用参数,将按字母顺序( ...

  9. 数组Array.sort()排序的方法

    数组sort排序 sort比较次数,sort用法,sort常用 描述 方法sort()将在原数组上对数组元素进行排序,即排序时不创建新的数组副本.如果调用方法sort()时没有使用参数,将按字母顺序( ...

随机推荐

  1. [BZOJ 1724] Fence Repair

    这大概是BZOJ里除了A+B Problem最水的一道题了吧 题面:http://www.lydsy.com/JudgeOnline/problem.php?id=1724 这道题其实有一些思路还是可 ...

  2. [LOJ2553]暴力写挂

    锟题x2 以下用$a\rightarrow b$表示端点为$a,b$的链 把式子写成$(h_1(x)+h_1(y)-h_1(lca))-h_2(lca')$,第一部分就是$x\rightarrow r ...

  3. 【Splay】Codeforces Round #424 (Div. 1, rated, based on VK Cup Finals) B. Cards Sorting

    Splay要支持找最左侧的最小值所在的位置.类似线段树一样处理一下,如果左子树最小值等于全局最小值,就查左子树:否则如果当前节点等于全局最小值,就查当前节点:否则查右子树. 为了统计答案,当然还得维护 ...

  4. 【动态规划+高精度】mr360-定长不下降子序列

    [题目大意] 韵哲君发现自己的面前有一行数字,当她正在琢磨应该干什么的时候,这时候,陈凡老师从天而降,走到了韵哲君的身边,低下头,对她耳语了几句,然后飘然而去. 陈凡老师说了什么呢,陈凡老师对韵哲君说 ...

  5. [转]Java程序员从笨鸟到菜鸟之(八十三)细谈Spring(十二)OpenSessionInView详解及用法

    首先我们来看一下什么是OpenSessionInView?    在hibernate中使用load方法时,并未把数据真正获取时就关闭了session,当我们真正想获取数据时会迫使load加载数据,而 ...

  6. GMT-CMSP系统维护步骤整理

    一.关闭前端各服务1.北京代理,韩国代理节点nginx/etc/init.d/nginx stop2.关闭WEB1,WEB2 NGINX和PHPpkill nginx && pkill ...

  7. jquer回显选中select下拉框

    公司使用的框架比较旧,没有使用el等表达式. <% String context = request.getContextPath(); String index = (String)reque ...

  8. iOS:ShareSDk的分享

    使用分享类的SDK其实有很多,例如友盟.ShareSDK等等,参照他们的文档集成起来并不是很难,可能出的一些问题也就是配置文件的问题,这里我个人使用了ShareSDK分享,具体操作可出现的问题如下: ...

  9. wireshark常用错误提示分析-转

    1.[Packet size limited during capture] 当你看到这个提示,说明被标记的那个包没有抓全.以图1的4号包为例,它全长有171字节,但只有前96个字节被抓到了,因此Wi ...

  10. unity color space 选取

    https://unity3d.com/cn/learn/tutorials/topics/graphics/choosing-color-space https://docs.unity3d.com ...