Possible Duplicates:
Sort objects using predefined list of sorted values
C# Help: Sorting a List of Objects in C#可能的重复:使用预定义的排序值列表对对象排序C#Help:在C#中排序对象列表
Double Post
Sorting a List of objects in 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?
这是一个列表,我试图找出一种有效的方法来排序列表
Thanks
谢谢
4 个解决方案
#1
42
CarList = CarList.OrderBy( x => x.CreationDate ).ToList();
#2
5
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();
#3
4
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个对象,以便排序算法知道如何对它们进行排序。你可以这两种方式之一:
- Make your
CarSpecs
class implement theIComparable
interface - 使您的CarSpecs类实现IComparable接口
- Create a class that implements
IComparer
and pass that in as a parameter toArray.Sort()
along with your array. - 创建一个实现IComparer的类,并将其作为参数传递给Array.Sort()以及您的数组。
#4
0
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()。实现一个IComparer类,使用CreationDate确定排序顺序,然后将所有对象放入数组或列表中,并调用相应的Sort()方法
#1
42
CarList = CarList.OrderBy( x => x.CreationDate ).ToList();
#2
5
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();
#3
4
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个对象,以便排序算法知道如何对它们进行排序。你可以这两种方式之一:
- Make your
CarSpecs
class implement theIComparable
interface - 使您的CarSpecs类实现IComparable接口
- Create a class that implements
IComparer
and pass that in as a parameter toArray.Sort()
along with your array. - 创建一个实现IComparer的类,并将其作为参数传递给Array.Sort()以及您的数组。
#4
0
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()。实现一个IComparer类,使用CreationDate确定排序顺序,然后将所有对象放入数组或列表中,并调用相应的Sort()方法