C# 对List成员排序的简单方法
网上看到的方法,实在太方便了,转过来保存,原链接:
http://blog.csdn.net/wanzhuan2010/article/details/6205884
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ListSort
{
class Program
{
static void Main(string[] args)
{
List<Customer> listCustomer = new List<Customer>();
listCustomer.Add(new Customer { name = "客户1", id = });
listCustomer.Add(new Customer { name = "客户2", id = });
listCustomer.Add(new Customer { name = "客户3", id = });
listCustomer.Add(new Customer { name = "客户4", id = });
listCustomer.Add(new Customer { name = "客户5", id = });
listCustomer.Add(new Customer { name = "客户6", id = });
///升序
List<Customer> listCustomer1 = listCustomer.OrderBy(s => s.id).ToList<Customer>();
//降序
List<Customer> listCustomer2 = listCustomer.OrderByDescending(s => s.id).ToList<Customer>();
//Linq排序方式
List<Customer> listCustomer3 = (from c in listCustomer
orderby c.id descending //ascending
select c).ToList<Customer>();
Console.WriteLine("List.OrderBy方法升序排序");
foreach (Customer customer in listCustomer1)
{
Console.WriteLine(customer.name);
}
Console.WriteLine("List.OrderByDescending方法降序排序");
foreach (Customer customer in listCustomer2)
{
Console.WriteLine(customer.name);
}
Console.WriteLine("Linq方法降序排序");
foreach (Customer customer in listCustomer3)
{
Console.WriteLine(customer.name);
}
Console.ReadKey();
}
}
class Customer
{
public int id { get; set; }
public string name { get; set; }
}
}
效果展示:

C# 对List成员排序的简单方法的更多相关文章
- Java中对List<E>按E的属性排序的简单方法
这是LeetCode上的题目56. Merge Intervals中需要用到的, 简单来说,定义了E为 /** * Definition for an interval. * public class ...
- Python判断列表是否已排序的各种方法及其性能分析
目录 Python判断列表是否已排序的各种方法及其性能分析 声明 一. 问题提出 二. 代码实现 2.1 guess 2.2 sorted 2.3 for-loop 2.4 all 2.5 numpy ...
- 提高C++程序运行效率的10个简单方法
转载: 一.尽量减少值传递,多用引用来传递参数.至于其中的原因,相信大家也很清楚,如果参数是int等语言自定义的类型可能能性能的影响还不是很大,但是如果参数是一个类的对象,那么其效率问题就不言而喻了. ...
- [Swift]八大排序算法(三):选择排序 和 简单选择排序
排序分为内部排序和外部排序. 内部排序:是指待排序列完全存放在内存中所进行的排序过程,适合不太大的元素序列. 外部排序:指的是大文件的排序,即待排序的记录存储在外存储器上,待排序的文件无法一次装入内存 ...
- c# 类成员的定义 定义方法、字段和属性【转】
c# 类成员的定义 定义方法.字段和属性c#类的成员包括字段.属性和方法.所有成员都有自己的访问级别,用下面的关键字之一来定义:public----成员可以有任何代码访问:private----成员只 ...
- Xcode7使用插件的简单方法&&以及怎样下载到更早版本的Xcode
Xcode7自2015年9上架以来也有段时间了, 使用Xcode7以及Xcode7.1\Xcode7.2的小伙伴会发现像VVDocumenter-Xcode\KSImageNamed-Xcode\HO ...
- chm转换为html的超简单方法
在Windows下chm转换为html的超简单方法(反编译CHM文件的方法) 通过调用Windows命令,将chm 文件转换为html 文件. 方法: 命令行(cmd),输入hh -decompile ...
- JS去掉首尾空格 简单方法大全(原生正则jquery)
JS去掉首尾空格 简单方法大全 var osfipin= ' http://www.cnblogs.com/osfipin/ '; //去除首尾空格 osfipin.replace(/(^\s*)|( ...
- python反转字符串(简单方法)及简单的文件操作示例
Python反转字符串的最简单方法是用切片: >>> a=' >>> print a[::-1] 654321 切片介绍:切片操作符中的第一个数(冒号之前)表示切片 ...
随机推荐
- POJ 1458
#include <iostream> #include <string> #define MAXN 1000 using namespace std; string s_1; ...
- js 阻止冒泡 兼容性方法
function customstopPropagation(e){ var ev = e || window.event; if (ev.stopPropagation) { ev.stopProp ...
- 错误 1 无法嵌入互操作类型“Microsoft.Office.Interop.Excel.ApplicationClass”。请改用适用的接口
http://www.cnblogs.com/waitingfor/archive/2011/12/19/2293469.html
- ssh超时断开的解决方法
当用SSH Secure Shell连接Linux时,如果几分钟没有任何操作,连接就会断开,必须重新登陆才行,每次都重复相同的操作,很是烦人,本文总结了两种解决的方法. 方法1:更改ssh服务器的配置 ...
- 浅谈Spark Kryo serialization
原创文章,转载请注明: 转载自http://www.cnblogs.com/tovin/p/3833985.html 最近在使用spark开发过程中发现当数据量很大时,如果cache数据将消耗很多的内 ...
- C#DataGridView 美化
private void dataGridView(DataGridView dataGridView) { System.Windows.Forms.DataGridViewCellStyle da ...
- JavaWeb项目开发案例精粹-第4章博客网站系统-004Service层
1. package com.sanqing.service; import java.util.List; import com.sanqing.fenye.Page; import com.san ...
- wordpress编辑器无法切换/输入
出现问题:“可视化”和“文本”无法切换,编辑区也无法输入文字 解决方法: 打开wp-config.php,在 require_once(ABSPATH . 'wp-settings.php'); 后面 ...
- ECharts本地部署
将下载的包解压,再将包内文件copy到C:\inetpub\wwwroot下 在html中做如下引用: <script type="text/javascript" sr ...
- MyBatis学习总结_18_MyBatis与Hibernate区别
也用了这么久的Hibernate和MyBatis了,一直打算做一个总结,就他们之间的优缺点说说我自己的理解: 首先,Hibernate是一个ORM的持久层框架,它使用对象和我们的数据库建立关系,在Hi ...