msdn List sort排序 IComparable 用法
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 用法的更多相关文章
- List<T>.Sort() 排序的用法
List<T> 可以通过 .Sort()进行排序,但是当 T 对象为自定义类型时(比如自定义模型),就需要 IComparable接口重写其中的方法来实现,实现代码如下: class Pr ...
- js数组之sort排序的用法
sort排序 转载自:https://blog.csdn.net/idomyway/article/details/80544509 js中用方法sort()为数组排序.sort()方法有一个可选参数 ...
- C++ 中的sort()排序函数用法
sort(first_pointer,first_pointer+n,cmp) 该函数可以给数组,或者链表list.向量排序. 实现原理:sort并不是简单的快速排序,它对普通的快速排序进行了优化,此 ...
- C#之IComparable用法,实现List<T>.sort()排序
这篇文章主要介绍了C#的一些基础知识,主要是IComparable用法,实现List<T>.sort()排序,非常的实用,这里推荐给大家. List<T>.sort()可以 ...
- JS基础篇--sort()方法的用法,参数以及排序原理
JS基础篇--sort()方法的用法,参数以及排序原理 sort() 方法用于对数组的元素进行排序,并返回数组.默认排序顺序是根据字符串Unicode码点.语法:arrayObject.sort( ...
- js sort() 排序用法(转载)
原文:https://blog.csdn.net/m0_37885651/article/details/80016718 sort() 方法用于对数组的元素进行排序,并返回数组.默认排序顺序是根据字 ...
- 反向输出及sort排序
建立条件:#include "algorithm"引用这个头文件 1.reverse 的用法,反向排序,由自己输入5个数: 1 2 3 4 5 for (int i = 0; i ...
- javascript:算法之数组sort排序
数组sort排序 sort比较次数,sort用法,sort常用 描述 方法sort()将在原数组上对数组元素进行排序,即排序时不创建新的数组副本.如果调用方法sort()时没有使用参数,将按字母顺序( ...
- 数组Array.sort()排序的方法
数组sort排序 sort比较次数,sort用法,sort常用 描述 方法sort()将在原数组上对数组元素进行排序,即排序时不创建新的数组副本.如果调用方法sort()时没有使用参数,将按字母顺序( ...
随机推荐
- JZYZOJ1383 [usaco2003feb]impster 位运算 最短路
http://172.20.6.3/Problem_Show.asp?id=1383 找能到达某个状态的最小操作数,然后把所有状态扫一遍即可,要额外判定一下起始就有的状态(如果起始里没有0那么这些状 ...
- HDU 6040 Hints of sd0061(划分高低位查找)
[题目链接] http://acm.hdu.edu.cn/showproblem.php?pid=6040 [题目大意] 给出一个随机数生成器,有m个询问,问第bi小的元素是啥 询问中对于bi< ...
- 【数论】【快速幂】【扩展欧几里得】【BSGS算法】bzoj2242 [SDOI2011]计算器
说是BSGS……但是跟前面那题的扩展BSGS其实是一样的……因为模数虽然是质数,但是其可能可以整除a!!所以这两者其实是一样的…… 第一二种操作不赘述. #include<cstdio> ...
- Problem G: 零起点学算法102——删除字符
#include<stdio.h> #include<string.h> int main() { ],a; while(gets(ch)!=NULL) { scanf(&qu ...
- hdu3401 Trade 单调队列优化dp
Trade Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Subm ...
- [典型漏洞分享]一个典型的软件漏洞--memcpy导致的缓冲区溢出
YS VTM模块存在缓冲区溢出漏洞,可导致VTM进程异常退出[高] 问题描述: YS VTM模块开放对外监听端口(8554和8664),并从外部接收网络数据,中间模块调用到memcpy函数对网络数据进 ...
- datatables .fnDraw is not a function
Datatables中文网:http://dt.thxopen.com/manual/api.html API DataTables 提供的可以操作表格数据的API,有下面六个关键部分: 表格(tab ...
- Bower管理依赖库初体验
比如一开始我用了jquery-1.10.2.min.js,后来要用bootstrap,但bootstrap依赖的确实2.0.3版本的jquery,那又要下载一个去替换原来的,这样的事情发生多了就会觉得 ...
- 以下内容为Stackoverflow上整理以作纪录
PRO 用IMG标签 Use IMG plus alt attribute if the image is part of the content such as a logo or diagram ...
- 判断客户端是PC还是手持设备的JS代码【转】
1.第一种: 复制代码代码如下: function IsPC() { var userAgentInfo = navigator.userAgent; var Agents = [&quo ...