• List排序


    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    
    namespace ConsoleApp
    {
        public class Student
        {
            public string Name;
            public int Score;
            public Student(string name, int score)
            {
                this.Name = name;
                this.Score = score;
            }
        }
    
        public class ListSortTest
        {
    
            public void sort() 
            {
                //元数据
                #region
                List<Student> students = new List<Student>()  { new Student("Student A", 90),
                                                                new Student("Student B", 75),
                                                                new Student("Student C", 83),
                                                                new Student("Student D", 94),
                                                                new Student("Student E", 60),
                                                                new Student("Student X", 60),
                                                                new Student("Student Y", 60),
                                                                new Student("Student F", 56),
                                                                new Student("Student G", 30),
                                                                new Student("Student I", 73),
                                                                new Student("Student J", 68),
                                                                new Student("Student K", 46)};
    
                foreach (var stu in students)
                {
                    Console.WriteLine("Name: {0}, Score: {1} ", stu.Name, stu.Score);
                }
                #endregion
    
                ////排序1
                //students.Sort((x, y) => { return -x.Score.CompareTo(y.Score); });
                students.Sort(delegate(Student a, Student b)
                {
                    int xdiff = -a.Score.CompareTo(b.Score);
                    if (xdiff != 0)
                        return xdiff;
                    else
                        return a.Name.CompareTo(b.Name);
                });
    
                // ---- 排序後(分數由高到低) (若相同按名称)
                Console.WriteLine("
    ----------排序後(分數由高到低,若相同按名称)-------
    ");
                foreach (var stu in students)
                    Console.WriteLine("Name: {0}, Score: {1} ", stu.Name, stu.Score);
    
    
                ////排序2
                //var v = from c in students
                //        orderby c.Score descending, c.Name descending
                //        select c;
                //students = v.ToList();
    
                //// ---- 排序後(分數由高到低) (若相同按名称)
                //Console.WriteLine("
    ----------排序後(分數由高到低,若相同按名称)-------
    ");
    
                //foreach (var stu in students)
                //    Console.WriteLine("Name: {0}, Score: {1} ", stu.Name, stu.Score);
    
    
                #region
                //// ---- 不及格的學生
                //Console.WriteLine("
    ------------不及格的學生------------
    ");
                //var query = from stu in students
                //            where stu.Score < 60
                //            select stu;
    
                //foreach (var q in query)
                //    Console.WriteLine("Name: {0}, Score: {1} ", q.Name, q.Score);
                #endregion
    
            }
        }
    
    
    }

     http://www.dotblogs.com.tw/timchang/archive/2012/10/24/78823.aspx
    http://stackoverflow.com/questions/289010/c-sharp-list-sort-by-x-then-y

  • 相关阅读:
    bzoj4518[Sdoi2016]征途 斜率优化dp
    bzoj3675[Apio2014]序列分割 斜率优化dp
    bzoj3437小P的牧场 斜率优化dp
    bzoj3156防御准备 斜率优化dp
    bzoj1911[Apio2010]特别行动队 斜率优化dp
    bzoj5100 [POI2018]Plan metra 构造
    bzoj1597[Usaco2008 Mar]土地购买 斜率优化dp
    刷题总结——Middle number(ssoj 优先队列)
    刷题总结——doing homework again(hdu1789)
    NOIP2017赛前模拟(3):总结
  • 原文地址:https://www.cnblogs.com/streetpasser/p/3442181.html
Copyright © 2020-2023  润新知