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

  • 相关阅读:
    jsp int转String or String转int 方法
    log4j详细使用教程
    SQL 查询当天,本月,本周的记录
    1012Linux流编程的一些知识点
    myeclipse过期以后提示过期以后怎么办?!
    mysql常用命令
    Myeclipse文件没出错,但是项目上显示有错的解决办法
    java的一些命名规范吧
    mysql 按照时间查询
    struts1.x和struts2.x之间的一些区别
  • 原文地址:https://www.cnblogs.com/streetpasser/p/3442181.html
Copyright © 2011-2022 走看看