Click here to Skip to main content
13,143,459 members (31,669 online)
Click here to Skip to main content

Stats

59.2K views
26 bookmarked
Posted 12 Aug 2009

How To Sort Generic List?

, 12 Aug 2009
An article on sorting generic list based on any attribute of user defined class
GenericListSorting
GenericListSorting
bin
Debug
GenericListSorting.exe
GenericListSorting.vshost.exe
GenericListSorting.vshost.exe.manifest
Properties
GenericListSorting.gpState
GenericListSorting.suo
using System;
using System.Collections.Generic;
using System.Text;

namespace GenericListSorting
{
    /// <summary>
    /// This class represents the structure, group of this structure is to be sorted.
    /// </summary>
    public class Employee : IComparable<Employee>
    {
        #region Private Members

        private Int32 empId;
        private String empName;
        private UInt32 empAge;

        #endregion

        #region Properties
        
        /// <summary>
        /// Gets or sets the employee id / no
        /// </summary>
        public Int32 EmployeeId
        {
            get { return empId; }
            set { empId = value; }
        }

        /// <summary>
        /// Gets or sets the name of the employee
        /// </summary>
        public String EmployeeName
        {
            get { return empName; }
            set { empName = value; }
        }

        /// <summary>
        /// Gets or sets the Age of the employee
        /// </summary>
        public UInt32 EmployeeAge
        {
            get { return empAge; }
            set { empAge = value; }
        }

        #endregion

        #region Constructor
        
        /// <summary>
        /// Creates an instance of an employee class.
        /// </summary>
        /// <param name="id"> Id / no of the emplyoee to be created.</param>
        /// <param name="name">Name of the employee to be created.</param>
        /// <param name="age">Age of the employee to be created.</param>
        public Employee(Int32 id, String name, UInt32 age)
        {
            this.empId = id;
            this.empName = name;
            this.empAge = age;
        }
        
        #endregion

        #region Static Members
        /// <summary>
        /// Comparison of age between the employees.
        /// </summary>
        public static Comparison<Employee> AgeComparison = delegate(Employee p1, Employee p2)
        {
            return p1.empAge.CompareTo(p2.empAge);
        };

        /// <summary>
        /// Comparison of name between the employees.
        /// </summary>
        public static Comparison<Employee> NameComparison = delegate(Employee p1, Employee p2)
        {
            return p1.empName.CompareTo(p2.empName);
        };

        /// <summary>
        /// Comparison of employee Id / No between the employees.
        /// </summary>
        public static Comparison<Employee> IDComparison = delegate(Employee p1, Employee p2)
        {
            return p1.empId.CompareTo(p2.empId);
        };

        #endregion

        #region IComparable<Product> Members
        
        /// <summary>
        /// Compares the current employee object with another object of the same type.
        /// </summary>
        /// <param name="other">An employee object to compare with this object</param>
        /// <returns>A 32-bit signed integer that indicates the relative order of the objects being compared.</returns>
        public Int32 CompareTo(Employee other)
        {
            return EmployeeName.CompareTo(other.EmployeeName);
        }

        #endregion

        #region Overridden Methods
        
        /// <summary>
        /// Gets a canonical string representation for the specified employee instance.
        /// </summary>
        /// <returns>A System.String instance that contains the unescaped canonical representation of the employee instance</returns>
        public override string ToString()
        {
            return string.Format("Id: {0} Name: {1} Age: {2}", empId, empName, empAge);
        }

        #endregion
    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

Anand Malli
Software Developer (Senior)
India India
Anand has been designing and developing applications using C# and the .NET framework since last 3 years.

When he's not writing code, you can catch him taking snaps or lost in playing computer games.

You may also be interested in...

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.170915.1 | Last Updated 12 Aug 2009
Article Copyright 2009 by Anand Malli
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid