繁体   English   中英

如何对学生列表进行排序?

[英]How Do I Sort a list of students?

如何通过不创建另一个cs文件按名称或按年份排序列表? 这种方法似乎根本不起作用。

case 6:
{         //Sorting                 
    do
    {                       
        Console.WriteLine("1. Sort by Name");
        Console.WriteLine("2. Sort by Age");
        int choice = int.Parse(Console.ReadLine());

        switch(choice)
        {                           
            case 1://Sort by Name

               st.Sort((a,b) => a.getName().CompareTo(b.getName()));
               ok = 1;
               break;

            case 2://Sort by Age

               st.Sort((a,b) => a.getAge().CompareTo(b.getAge()));
               ok = 1;
               break;                           
            default:Console.WriteLine("Not in the choices!"); ok = 0;
               break;                           
         }                      
    }
    while (ok != 1);
}
break;//case6-Sort

使用LINQ应该更容易:

var sorted = list.OrderBy(person => person.getName())
                 .ToList();

如果你想按一个属性排序只使用OrderBy ,如果你想进一步对列表进行排序,以防有几个具有相同值的项目,请使用ThenBy

var sorted = list.OrderBy(person => person.getName())
                 .ThenBy(person => person.getAge())
                 .ToList();

使用Linq

按名称排序

var students = st.OrderBy(s => s.Name).ToList();

按年龄排序

var students = st.OrderBy(s => s.Age).ToList();

您可以使用LINQ:

// sort by name
var newList = st.OrderBy(a => a.getName()).ToList();

// sort by age
var newList = st.OrderBy(a => a.getAge()).ToList();

如果您特别想要进行就地排序列表,请不要使用Linq。 这是一个不合适的解决方案(似乎人们建议它只是因为“它更新”)。

无论如何,关于你的问题。 你的代码应该有效。 这是一个可编辑的控制台应用程序,它演示了它的工作原理。

查看此代码并将其与您正在执行的操作进行比较。 我怀疑你的问题在于你没有向我们展示的代码区域。

using System;
using System.Collections.Generic;

namespace Demo
{
    class Person
    {
        private readonly string name;
        private readonly int    age;

        public Person(string name, int age)
        {
            this.name = name;
            this.age = age;
        }

        public string getName()
        {
            return name;
        }

        public int getAge()
        {
            return age;
        }
    }

    internal class Program
    {
        private static void Main(string[] args)
        {
            var persons = new List<Person>
            {
                new Person("C", 4),
                new Person("D", 1),
                new Person("A", 3),
                new Person("B", 2)
            };

            persons.Sort((a,b) => a.getName().CompareTo(b.getName()));
            print(persons); // Ordered by name "A", "B", "C", "D".

            persons.Sort((a, b) => a.getAge().CompareTo(b.getAge()));
            print(persons);  // Ordered by age "1", "2", "3", "4".
        }

        private static void print(List<Person> persons)
        {
            foreach (var person in persons)
                Console.WriteLine("Name: {0}, Age: {1}", person.getName(), person.getAge());

            Console.WriteLine();
        }
    }
}

Linq是最新的编程实践,你应该在你的情况下使用它:

case 6:{//Sorting

                do{

                    Console.WriteLine("1. Sort by Name");
                    Console.WriteLine("2. Sort by Age");
                    int choice = int.Parse(Console.ReadLine());

                    switch(choice){

                        case 1://Sort by Name

                            students = st.OrderBy(s=>s.name);
                            ok = 1;
                            break;

                        case 2://Sort by Age

                            students = st.OrderBy(s=>s.age);
                            ok = 1;
                            break;

                        default:Console.WriteLine("Not in the choices!"); ok = 0;break;

                    }

                }while(ok != 1);}break;//case6-Sort

你为什么不试试

排序列表

希望能帮到你。

您可以使用

//Sort by Name
emp = emp.OrderBy(x => x.Name).ToList();

//Sort by Age
emp = emp.OrderBy(x => x.Age).ToList(); 

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM