12

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#

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

Community
  • 1
  • 1

4 Answers4

42
CarList = CarList.OrderBy( x => x.CreationDate ).ToList();
Joel Coehoorn
  • 399,467
  • 113
  • 570
  • 794
  • 1
    Coehoom what is x? is it lambda? how to define it? – Aditya Patil Jul 14 '14 at 07:27
  • 1
    @AdityaPatil x is a placeholder for individual items in the CarList sequence. `x => x.CreationDate` is an expression that tells the `OrderBy()` function what criteria to use for sorting the items. It means to sort by the CreationDate property of each item. – Joel Coehoorn Jul 14 '14 at 13:42
  • 1
    its technically called Lambda Expression right? – Aditya Patil Jul 15 '14 at 04:09
5

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

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:

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

var lambda = CarList.OrderBy(c => c.CreationDate).ToList();
Tomas Aschan
  • 58,548
  • 56
  • 243
  • 402
4

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

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:

  1. Make your CarSpecs class implement the IComparable interface
  2. Create a class that implements IComparer and pass that in as a parameter to Array.Sort() along with your array.
Dan Herbert
  • 99,428
  • 48
  • 189
  • 219
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

thecoop
  • 45,220
  • 19
  • 132
  • 189