Labels

Monday, March 9, 2009

IComparer Myth

Hi,

 

Take a look at the below snippet which implements the custom IComparer to compare ‘Movie’ type of Objects.

 

public class Movie

{

            public string Name;

            public string Language;

 

            public Movie()

            {

            }

 

            public Movie(string name, string language)

            {

                        This.Name = name;

                        This.Language = language;

            }

}

 

public class MovieComparer : System.Collections.Generic.IComparer<Movie>

{

            public string sortExpression;

            public string sortOrder;

 

 

            public int Compare(Movie x, Movie y)

            {

                        //return obj1.Name.CompareTo(obj2.Name);  // Simple Way

 

                        try

                        {

                                    switch (SortOrder)

                                    {

                                                case "asc":

                                                            if (SortExpression.Equals("Name", StringComparison.OrdinalIgnoreCase))

                                                                        return String.Compare(x.Name, y.Name);

                                                            else if (SortExpression.Equals("Language", StringComparison.OrdinalIgnoreCase))

                                                                        return String.Compare(x.Language, y.Language);

                                                            break;

 

                                                case "dsc":

                                                            if (SortExpression.Equals("Name", StringComparison.OrdinalIgnoreCase))

                                                                        return String.Compare(y.Name, x.Name);

                                                            else if (SortExpression.Equals("Language", StringComparison.OrdinalIgnoreCase))

                                                                        return String.Compare(y.Language, x.Language);

                                                            break;

 

                                                default:

                                                            break;

                                    }

                                    return 0;

                        }

                        catch (ArgumentException ex)

                        {

                                    throw ex;

                        }

            }

}

 

static void Main()

{

        List<Movie> list = Session["Data"] as List<Movie>;

        List.add(new Movie(‘x’,’y’);

        List.add(new Movie(‘x1’,’y1’);

         --------------

 

        MovieComparer movieComparer = new MovieComparer();

        movieComparer.SortExpression = sortExpression;

        movieComparer.SortOrder = direction;

        list.Sort(movieComparer); 

 

        Session["Data"] = list;

        GridView1.DataSource = Session["Data"] as List<Movie>;

        GridView1.DataBind();

}

 

 

-          By default, the Sort() method uses the IComparable implementation for each element to handle the comparisons necessary for sorting.

-          However, sometimes, though, it is useful to sort the same list in different ways.

-          To accomplish this, an overload of Sort(IComparer comparer) exists that takes an IComparer as a parameter; where IComparer.Compare is then used for the comparisons.

-          This overload allows users of the class to use any of the built-in IComparers or any of their own making (here MovieComparer).

 

 

 

Regards,

Arun Manglick

No comments:

Post a Comment