zoukankan      html  css  js  c++  java
  • 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

  • 相关阅读:
    6 docker-harbor仓库搭建
    4 dockerfile介绍及其实例应用
    1 docker 介绍和安装
    2 docker镜像
    PAT甲级1075 PAT Judge
    PAT甲级1139 First Contact【离散化】
    PAT甲级1055 The World's Richest【排序】
    PAT甲级1013-1014-1015
    洛谷P1135 奇怪的电梯【bfs】
    洛谷P1182 数列分段【二分】【贪心】
  • 原文地址:https://www.cnblogs.com/streetpasser/p/3442181.html
Copyright © 2011-2022 走看看