C# 帮助:在 C# 中对对象列表进行排序

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/925571/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 03:22:04  来源:igfitidea点击:

C# Help: Sorting a List of Objects in C#

c#sortingclass

提问by

Possible Duplicates:
Sort objects using predefined list of sorted values
C# Help: Sorting a List of Objects in C#

Double Post

Sorting a List of objects in C#

可能的重复项:
使用预定义的排序值列表对对象进行排序
C# 帮助:在 C# 中对对象列表进行排序

双柱

在 C# 中对对象列表进行排序

public class CarSpecs
{

    public CarSpecs()
    {
    }

    private String _CarName;
    public String CarName
    {
        get { return _CarName; }
        set { _CarName = value; }
    }



    private String _CarMaker;
    public String CarMaker
    {
       get { return _CarMaker;}
       set { _CarMaker = value; }
    }


    private DateTime _CreationDate;
    public DateTime CreationDate
    {
        get { return _CreationDate; }
        set { _CreationDate = value; }
    }
}

This is a list and I am trying to figure out an efficient way to sort this list List<CarSpecs> CarList, containing 6(or any integer amount) Cars, by the Car Make Date. I was going to do Bubble sort, but will that work? Any Help?

这是一个列表,我试图找出一种有效的方法来List<CarSpecs> CarList按汽车制造日期对包含 6 个(或任何整数数量)汽车的列表进行排序 。我打算做冒泡排序,但这行得通吗?任何帮助?

Thanks

谢谢

回答by thecoop

There are already existing sorting algorithms in the BCL - Array.Sort() and List.Sort(). Implement an IComparer class that determines the sort order using the CreationDate, then put all the objects into an array or list and call the appropriate Sort() method

BCL 中已经存在排序算法 - Array.Sort() 和 List.Sort()。实现使用 CreationDate 确定排序顺序的 IComparer 类,然后将所有对象放入数组或列表中并调用适当的 Sort() 方法

回答by Joel Coehoorn

CarList = CarList.OrderBy( x => x.CreationDate ).ToList();

回答by Dan Herbert

Don't write your own sorting algorithm. .NET has an Array.Sort()method specifically for things such as this.

不要编写自己的排序算法。.NET 有一种Array.Sort()专门用于此类事情的方法。

Since you have a custom object, you need to define how to compare 2 objects so the sorting algorithm knows how to sort them. You can do this 1 of 2 ways:

由于您有一个自定义对象,您需要定义如何比较 2 个对象,以便排序算法知道如何对它们进行排序。您可以通过以下 2 种方式之一执行此操作:

  1. Make your CarSpecsclass implement the IComparableinterface
  2. Create a class that implements IComparerand pass that in as a parameter to Array.Sort()along with your array.
  1. 让你的CarSpecs类实现IComparable接口
  2. 创建一个实现IComparer并将其作为参数Array.Sort()与数组一起传递的类。

回答by Tomas Aschan

First, using the shorthand syntax introduced in .Net 3.5, you could make this class definition a lot shorter:

首先,使用 .Net 3.5 中引入的速记语法,您可以使此类定义更短:

public class CarSpecs
{
    public CarSpecs() { }

    public String CarName { get; set;
    public String CarMaker { get; set; }
    public DateTime CreationDate { get; set; }
}

This will compile into the exact same class as the one you have above.

这将编译成与上面的类完全相同的类。

Secondly, you can easily sort them using Lambda expressions or LINQ:

其次,您可以使用 Lambda 表达式或 LINQ 轻松对它们进行排序:

var linq = (from CarSpecs c in CarList
            orderby c.CreationDate ascending
            select c) as List<CarList>;

var lambda = CarList.OrderBy(c => c.CreationDate).ToList();