ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • A Short History of Sorting in C#
    IT Story/C# & WPF 2019. 12. 5. 10:40
    반응형

    Recently, while reading Jon Skeet’s excellent book“C# in Depth” I came across the long and varied history of sorting in the C# language. It provides a tantalising view of how the language has evolved over the years. Apart from being a great example on C# in particular it’s also a good example on how languages have improved over the years to become more expressive, while maintaining backward compatibility with earlier versions.

    Take the simple act of sorting a list of objects, like for example products, shown here.

     

    internal class Product
    {
        public string Name { get; set; }
        public decimal Price { get; set; }
     
        public static List<Product> GetSampleProducts()
        {
            return new List<Product>
            {
                new Product { Name="Watership Down", Price = 6.25m },
                new Product { Name="The Color of Magic", Price = 7.00m },
                new Product { Name="Guards! Guards!", Price = 12.00m },
                new Product { Name="War and Peace", Price = 23.00m }    
            };
        }
    }

    Now you could go old school and implement your own [enter favourite algorithm here] sort routine, but for the more pedestrian programmer bent on actually getting things done, a more pragmatic approach in order. Imagine that your stuck on C# 1.0. You have an ArrayList of Product objects that you want to sort by Name. You could use the ArrayList.Sort method along with an IComparer object like so.

     

    private class ProductNameComparer : IComparer
    {
        public int Compare(object x, object y)
        {
            Product first = x as Product;
            Product second = y as Product;
     
            if (x == null || y == null)
                throw new InvalidCastException("Objects passed in are not Products.");
            return first.Name.CompareTo(second.Name);
        }
    }
     
    ArrayList products = new ArrayList(Product.GetSampleProducts());
    products.Sort(new ProductNameComparer());

    You could get the Product class itself to implement the IComparer interface, but then your stuck with sorting Product objects with just one field, i.e. in this case the name. Using an externally implemented class allows you to choose which Product field you want to target for the sort. You could very well create a ProductPriceComparer object that will be used by the Sort routine to compare the prices of a product.

    There are a couple of things that could be improved with this method though. First, there is the verbosity. That’s a lot of code just to tell the language to sort something in alphabetical order. Then there are the casts within the IComparer.Compare method. This is probably something that you want to avoid as casting can result in runtime errors if the objects in the array list are not what you expected them to be. Note, as aside, I’ve used “as” to cast as opposed to the Product first = (Product)x style cast as its considered safer. Fortunately things improve in C# 2 with Generics.

    반응형

    'IT Story > C# & WPF' 카테고리의 다른 글

    How to create REST service  (0) 2019.12.12
    How to select certain Elements  (0) 2019.12.05
    How to use C# ArrayList Class  (0) 2019.12.05
    Create a Numeric Text Box  (0) 2019.12.03
    C# String to Number Conversion  (0) 2019.12.03

    댓글

Designed by Tistory.