using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using xiketang.com.GenericList;namespace xiketang.com.ListDataShow
{public partial class FrmDgvList : Form{private List<Course> courseList = null;public FrmDgvList(){InitializeComponent();InitList();}public void InitList(){Course course1 = new Course();course1.CourseId = 10001;course1.CourseName = "C#面向对象中集合的应用";course1.ClassHour = 2;course1.Teacher = "常老师";Course course2 = new Course { CourseId = 10002, CourseName = "SQLServer数据库开发", ClassHour = 5, Teacher = "常老师" };Course course3 = new Course(10003, ".Net/C#全栈VIP课程", 200, "常老师");Course course4 = new Course(10004, ".Net/C#上位机VIP课程", 500, "常老师");Course course5 = new Course(10005, ".Net/C#高级进阶VIP课程", 300, "常老师");courseList = new List<Course>() { course1, course2, course3, course4, course5 };}private void btnShowData_Click(object sender, EventArgs e){this.dgvCourseList.DataSource = courseList;this.dgvCourseList.AutoResizeColumns();}private void btnCourseIdDESC_Click(object sender, EventArgs e){courseList.Sort(new CourseIdDESC());this.dgvCourseList.Refresh();}private void btnClassHourASC_Click(object sender, EventArgs e){courseList.Sort(new CourseClassASC());this.dgvCourseList.Refresh();}}
}using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;namespace xiketang.com.GenericList
{public class Course : IComparable<Course>{public Course() { }public Course(int courseId, string courseName, int classHour, string teacher){this.CourseId = courseId;this.CourseName = courseName;this.ClassHour = classHour;this.Teacher = teacher;}public int CourseId { get; set; }public string CourseName { get; set; }public int ClassHour { get; set; }public string Teacher { get; set; }public int CompareTo(Course other){return other.CourseId.CompareTo(CourseId);}}class TestGenericList{#region 对象集合添加和删除的几种方式 public List<Course> CreateCourses(){Course course1 = new Course();course1.CourseId = 10001;course1.CourseName = "C#面向对象中集合的应用";course1.ClassHour = 2;course1.Teacher = "常老师";Course course2 = new Course { CourseId = 10002, CourseName = "SQLServer数据库开发", ClassHour = 5, Teacher = "常老师" };Course course3 = new Course(10003, ".Net/C#全栈VIP课程", 200, "常老师");Course course4 = new Course(10004, ".Net/C#上位机VIP课程", 500, "常老师");Course course5 = new Course(10005, ".Net/C#高级进阶VIP课程", 300, "常老师");Course[] courseArray = new Course[5];courseArray[0] = course1;List<Course> courseList = new List<Course>() { course1, course2, course3, course4, course5 };Course[] courseArray1 = new Course[] { course1, course2, course3, course4, course5 };List<Course> courseListFromArray = new List<Course>();courseListFromArray.AddRange(courseArray1);Course[] courseArray2 = courseList.ToArray();List<Course> courseList3 = courseArray2.ToList();return courseList;}#endregion#region 集合元素的遍历和快速查找public void TraversalList1(List<Course> courseList){for (int i = 0; i < courseList.Count; i++){Console.WriteLine($"{courseList[i].CourseId}\t{courseList[i].CourseName}\t{courseList[i].ClassHour}\t{courseList[i].Teacher}");}}public void TraversalList2(List<Course> courseList){foreach (Course item in courseList){Console.WriteLine($"{item.CourseId}\t{item.CourseName}\t{item.ClassHour}\t{item.Teacher}");}}public void QueryElements(List<Course> courseList){List<Course> result1 = courseList.FindAll(c => c.CourseId > 10003);var result2 = from c in courseList where c.CourseId > 10003 select c;var result3 = result2.ToList();}#endregion#region 集合元素排序public void ListOrder(){#region 值类型元素的排序Console.WriteLine("\r\n----------------------值类型元素排序------------------\r\n");List<int> ageList = new List<int> { 20, 19, 25, 30, 26 };ageList.Sort();foreach (int item in ageList){Console.WriteLine(item);}ageList.Reverse();Console.WriteLine("-------------");foreach (int item in ageList){Console.WriteLine(item);}#endregionConsole.WriteLine("\r\n----------------------集合默认排序------------------\r\n");List<Course> courseList = CreateCourses();courseList.Sort();TraversalList1(courseList);Console.WriteLine("\r\n----------------------集合动态排序------------------\r\n");courseList.Sort(new CourseIdASC());Console.WriteLine("-----------按照编号升序---------\r\n");TraversalList1(courseList);Console.WriteLine("\r\n-----------按照编号将序---------\r\n");courseList.Sort(new CourseIdDESC());TraversalList1(courseList);Console.WriteLine("\r\n--------后面高级课程中,使用LINQ实现排序------------");var list1 = from c in courseList orderby c.CourseId ascending select c;TraversalList1(list1.ToList());Console.WriteLine("\r\n--------后面高级课程中,使用扩展方法OrderByDescending实现降序------------");var list2 = courseList.OrderByDescending(c => c.CourseId);TraversalList1(list2.ToList());Console.WriteLine("\r\n--------后面高级课程中,使用扩展方法OrderBy实现升序序------------");var list3 = courseList.OrderBy(c => c.ClassHour);TraversalList1(list3.ToList());}#endregion}#region 自定义排序类:根据需要,添加对应个数的排序类class CourseIdASC : IComparer<Course>{public int Compare(Course x, Course y){return x.CourseId.CompareTo(y.CourseId);}}class CourseIdDESC : IComparer<Course>{public int Compare(Course x, Course y){return y.CourseId.CompareTo(x.CourseId);}}class CourseClassASC : IComparer<Course>{public int Compare(Course x, Course y){return x.ClassHour.CompareTo(y.ClassHour);}}#endregion }