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

  • 相关阅读:
    mac 监控文件变化并重启php
    关闭某个命令的进程
    debian 添加永久环境变量方法
    debian swoole环境
    swoole 编程环境安装
    计算机网络知识笔记
    Mac 配置 php-fpm
    存储过程 利用游标 解决复制业务
    ubuntu 宝塔安装一条龙服务
    项目重零开始搭建
  • 原文地址:https://www.cnblogs.com/streetpasser/p/3442181.html
Copyright © 2011-2022 走看看