tags:

views:

158

answers:

4
+3  Q: 

Sort Generic List

I am wanting to sort a Generic List in Ascending Date order. (Framework v2)

Any suggestions?

Thanks

+2  A: 

Use List<T>.Sort(), and just make sure you have an appropriate comparer.

Wim Hollebrandse
+7  A: 

This is a nice tutorial it shows you how to do it.

Upul
I was going to answer the same thing, just great those delegates, aren't they?
NoProblemBabe
+1  A: 

Upul and Wim said it all:
Use delegates:

new List<DateTime>().Sort(delegate(DateTime d1, DateTime d2) {
    return d1.CompareTo(d2);
});

Or if you have a class or something that holds that datetime:

new List<Nhonho>().Sort(delegate(Nhonho n1, Nhonho n2) {
    return n1.date.CompareTo(n2.date);
});

To make it lessening,

new List<Nhonho>().Sort(delegate(Nhonho n1, Nhonho n2) {
    return n2.date.CompareTo(n1.date);
});

Good luck.

NoProblemBabe
+2  A: 

Hey guys! For this scenario I would suggest something quite similar but in my opinion a better way to solve this problem.

I would implement new classes that their only pourpouse is to have a particular sorting "strategy". For example; I would create "ProductNameComparer" and "ProductPriceComparer", these classes should implement IComparer interface, then I would only call the method sort passing these strategies as parameters. You may check the code I have just made to ilustrate my ponit;

public class ProductPriceComparer : IComparer<Product>
    {
        public int Compare(Product x, Product y)
        {
            if (x.Price > y.Price)
                return 1;

            if (x.Price < y.Price)
                return -1;

            return 0;
        }
    }

    public class ProductNameComparer : IComparer<Product>
    {
        public int Compare(Product x, Product y)
        {
            return x.Name.CompareTo(y.Name);
        }
    }

    public class Product
    {
        public Product(String name, Decimal price)
        {
            this.name = name;
            this.price = price;
        }

        private String name;
        public String Name
        {
            get { return name; }
            set { name = value; }
        }

        private Decimal price;
        public Decimal Price
        {
            get { return price; }
            set { price = value; }
        }

        public override string ToString()
        {
            return Name + " ($"+ Price.ToString() + ")";
        }
    }

    static void Main(string[] args)
    {
        List<Product> products = new List<Product>();
        products.Add(new Product("Product Z", 45.98m));
        products.Add(new Product("Product D", 12.80m));
        products.Add(new Product("Product A", 25.19m));
        products.Add(new Product("Product B", 65.00m));
        products.Add(new Product("Product P", 5.14m));

        Console.WriteLine("PRODUCTS SORTED BY PRICE");
        products.Sort(new ProductPriceComparer());
        foreach (Product p in products)
            Console.WriteLine(p.ToString());

        Console.WriteLine("\n--------------------------------\n");

        Console.WriteLine("PRODUCTS SORTED BY NAME");
        products.Sort(new ProductNameComparer());
        foreach (Product p in products)
            Console.WriteLine(p.ToString());
    }

What we are supossed to get is;

PRODUCTS SORTED BY PRICE Product P ($5,14) Product D ($12,80) Product A ($25,19) Product Z ($45,98) Product B ($65,00)


PRODUCTS SORTED BY NAME Product A ($25,19) Product B ($65,00) Product D ($12,80) Product P ($5,14) Product Z ($45,98)

I think its gonna help!!! See yaa!

Why not just implement IComparable on product?
Stevo3000
If I do so I'm gonna be always tied in having only one sorting method. And I may get a serious problem when using lots of "if" to modifiy the way I wanna sort (implicate in redability problems). Note that implementing IComparer in classes that has this only pourporse is nothing more than the Strategy Design Pattern (GoF) being used.