[C#基础]List的Sort()、Find()、FindAll()、Exist()的使用方法举例

List函数用得还是比较多的,正好用到其中的向个方法,做了一个例程,再总结一下:

先建一个学生类:

        public class student
{
public int Number { get; set; }
public string Name { get; set; }
public bool Sex { get; set; }
public student(int _number, string _name, bool _sex)
{
Number = _number;
Name = _name;
Sex = _sex;
}
public override string ToString()
{
return string.Format("序号:{0},姓名:{1},性别:{2}",
Number.ToString(), Name, Sex ? "男" : "女");
}
}

例程代码如下:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms; namespace ListSortTest
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
} List<student> Students = new List<student>();
private void Form1_Load(object sender, EventArgs e)
{ Students.Add(new student(1, "张一", true));
Students.Add(new student(3, "张二", false));
Students.Add(new student(5, "张三", true));
Students.Add(new student(2, "张四", false));
Students.Add(new student(4, "张五", true));
Students.Add(new student(6, "张六", false));
}
//排序按钮
private void button1_Click(object sender, EventArgs e)
{
richTextBox1.Text += "**原始显示:\r\n";
showList(Students); richTextBox1.Text += "\r\n**用序号排序从小到大显示:\r\n";
Students.Sort((x, y) => x.Number < y.Number ? -1 : 0);
showList(Students); richTextBox1.Text += "\r\n**用序号排序从大到小显示:\r\n";
Students.Sort((x, y) => x.Number > y.Number ? -1 : 0);
showList(Students); richTextBox1.Text += "\r\n**用姓名排序(升序)显示:\r\n";
Students.Sort((x, y) => x.Name.CompareTo(y.Name));
showList(Students); richTextBox1.Text += "\r\n**用姓名排序(降序)显示:\r\n";
Students.Sort((x, y) => y.Name.CompareTo(x.Name));
showList(Students); richTextBox1.Text += "\r\n**用性别排序(升序)显示:\r\n";
Students.Sort((x, y) => x.Sex.CompareTo(y.Sex));
showList(Students);
} private void showList(List<student> _list)
{
for (int i = 0; i < _list.Count; i++)
{
richTextBox1.Text += _list[i].ToString() + "\r\n";
}
} private void button2_Click(object sender, EventArgs e)
{
richTextBox1.Text += "\r\n**找出Name=\"张四\"的学生:\r\n";
richTextBox1.Text += Students.Find((student s) => s.Name == "张四").ToString(); richTextBox1.Text += "\r\n\r\n**找出第一个男学生:";
richTextBox1.Text += "(该方法只会找到第一个就停止)\r\n";
richTextBox1.Text += Students.Find((student s) => s.Sex == true).ToString(); richTextBox1.Text += "\r\n\r\n**找出所有女学生:\r\n";
showList(Students.FindAll((student s) => s.Sex == false)); richTextBox1.Text += "\r\n\r\n**判断“张四”学生是否存在:\r\n";
richTextBox1.Text += Students.Exists((student s) => s.Name == "张四" ? true : false).ToString(); }
}
}

通过以上代码测试,排序效果如下:

其它功能显示如图(欢迎访问http://www.cnblogs.com/dooroo)

[C#]List的Sort()、Find()、FindAll()、Exist()的使用方法举例的更多相关文章

  1. Python: 字符串搜索和匹配,re.compile() 编译正则表达式字符串,然后使用match() , findall() 或者finditer() 等方法

    1. 使用find()方法 >>> text = 'yeah, but no, but yeah, but no, but yeah' >>> text.find( ...

  2. Array.prototype.sort()对数组对象排序的方法

    Array.prototype.sort()方法接受一个参数——Function,Function会提供两个参数,分别是两个进行比较的元素,如果元素是String类型则通过Unicode code进行 ...

  3. java Collections.sort()实现List排序的默认方法和自定义方法

    1.java提供的默认list排序方法 主要代码: List<String> list = new ArrayList();list.add("刘媛媛"); list. ...

  4. ORA-12805: parallel query server died unexpectedly ORA-04030 (sort subheap,sort key) 原因排查与解决方法

    今日,某服务器pga调整为30G,_pga_max_size调整为8G之后(原来是2G,但是one passes语句较多,性能太低),执行出现ORA-12805: parallel query ser ...

  5. Mysql的“Table 'mysql.servers' doesn't exist”的解决方法

    安装MYSQL后,又一次系统出现问题了,于是我查看mysql的错误日志,竟发现Table 'mysql.servers' doesn't exist问题的错误, 虽然与我的问题无关,但这个问题还是引起 ...

  6. java Collections.sort()实现List排序的默认方法和自定义方法【转】

    1.java提供的默认list排序方法 主要代码: List<String> list = new ArrayList();list.add("刘媛媛"); list. ...

  7. phpcms网站迁移无法更新内容提示Table 'led_com.lz_' doesn't exist的解决方法

    新接的一位客户说要把旧phpcms网站迁移到新的服务器并更换新域名,这对ytkah是小菜一碟,但往往事与愿违,忽略了一些细节会很惨的.进入新站后台怎么都无法生成内容,提示Table 'led_com. ...

  8. re正则match、search、findall、finditer函数方法使用

    match 匹配string 开头,成功返回Match object, 失败返回None,只匹配一个. search 在string中进行搜索,成功返回Match object, 失败返回None, ...

  9. Linux下MySQL报Table 'xxx' doesn't exist错误解决方法,表名存在大小写区分

    Linux服务器上在线装了个MySQL,但是部署web应用时一直报后台一直报错:Table 'xxx' doesn't exist. 本地测试一直都是正常的,同样的代码,同样的数据库,表是存在的,但是 ...

随机推荐

  1. C#系列之聊聊.Net Core的InMemoryCache

    作者:暴王 个人博客:http://www.boydwang.com/2017/12/net-core-in-memory-cache/ 这两天在看.net core的in memory cache, ...

  2. Python系列:二、数据类型--技术流ken

    标准数据类型 Python3 中有六个标准的数据类型: Number(数字) String(字符串) List(列表) Tuple(元组) Set(集合) Dictionary(字典) Python3 ...

  3. SpringBoot解决ajax跨域问题

    一.第一种方式: 1.编写一个支持跨域请求的 Configuration import org.springframework.context.annotation.Configuration; im ...

  4. WPF 列表虚拟化时的滚动方式

    ListBox的滚动方式 分为像素滚动和列表项滚动 通过ListBox的附加属性ScrollViewer.CanContentScroll来设置.因此ListBox的默认模板中,含有ScrollVie ...

  5. asp.net-基础-20180320

    常用页面指令 <%@page%>:一个页面只能有一个 <%@Import NameSpace=“Value“%> 导入命名空间 <%@OutputCache%> 设 ...

  6. 【Java每日一题】20170223

    20170222问题解析请点击今日问题下方的“[Java每日一题]20170223”查看(问题解析在公众号首发,公众号ID:weknow619) package Feb2017; public cla ...

  7. The JRE_HOME environment variable is not defined correctly This environment

    昨天启动tomcat还好好的,今天不知道抽什么风,cmd中报错: The JRE_HOME environment variable is not defined correctly This env ...

  8. Chrome开发者工具Debug入门

    译者按: 手把手教你摆脱console.log,掌握高级的debug方法. 原文: Learn How To Debug JavaScript with Chrome DevTools 译者: Fun ...

  9. 缓存MEMCACHE php调用(一)

    在项目中,涉及大访问量时,合理的使用缓存能减轻数据库的压力,同时提升用户体验.即在非实时性的需求的前提下,一小段时间内(若干秒),用于显示的数据从缓存中获取的,而不用直接读取数据库,能有效的减少数据库 ...

  10. layui 自定义表单验证的几个实例

    *注:使用本方法请先引入layui依赖的layu.js和layui.css 1.html <input type="text" name="costbudget&q ...