list排序

http://www.cnblogs.com/dooroo/archive/2012/12/11/2813858.htmlhtml

        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();

        }
    }
}

 

 

經過以上代碼測試,排序效果以下:ide

其它功能顯示如圖(歡迎訪問http://www.cnblogs.com/dooroo)post

 

 
分類:  C#基礎
相關文章
相關標籤/搜索