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

  • 相关阅读:
    Go interface{}、类型断言
    相关资料
    php实践
    安装zookeeper
    对象池化,对象池
    java getResourcesAsStream()如何获取WEB-INF下的文件流
    android--SDK Manager下载Connection to http://dl-ssl.google.com refused
    Intellij idea 切换SVN路径
    Intellij Idea @Autowired取消提示
    恢复文件默认打开方式
  • 原文地址:https://www.cnblogs.com/streetpasser/p/3442181.html
Copyright © 2011-2022 走看看