I'm trying to sort a list of cars by their price range. Currently I'm unsure if I've even sorted it correctly with IComparable, and I can't figure out how I print the list after it's sorted, I can only print them in the order they were declared.
using System;
using System.Collections.Generic;
namespace Interface1
{
class Program
{
class Car : IComparable<Car>
{
public string Make { get; set; }
public string Model { get; set; }
public double Price { get; set; }
public int CompareTo(Car car)
{
return this.Price.CompareTo(car.Price);
}
static void Main(string[] args)
{
List<Car> cars = new List<Car>()
{
new Car(){Make = "Skoda", Model = "Fabia", Price = 50000},
new Car(){Make = "Skoda", Model = "Octavia", Price = 60000},
new Car(){Make = "Nissan", Model = "Juke", Price = 45000 }
};
foreach (var car in cars)
Console.WriteLine(car.Price);
}
}
}
}
Have I done the sorting correctly? How can I print it properly?