Sort by Name : Generic Collections « Generics « C# / C Sharp

Home
C# / C Sharp
1.2D Graphics
2.Class Interface
3.Collections Data Structure
4.Components
5.Data Types
6.Database ADO.net
7.Design Patterns
8.Development Class
9.Event
10.File Stream
11.Generics
12.GUI Windows Form
13.Language Basics
14.LINQ
15.Network
16.Office
17.Reflection
18.Regular Expressions
19.Security
20.Services Event
21.Thread
22.Web Services
23.Windows
24.Windows Presentation Foundation
25.XML
26.XML LINQ
C# / C Sharp by API
C# / CSharp Tutorial
C# / CSharp Open Source
C# / C Sharp » Generics » Generic CollectionsScreenshots 
Sort by Name
  

using System;
using System.Collections.ObjectModel;
using System.Collections.Generic;
using System.Text;
using System.Runtime.Serialization;


public class Employee : System.IComparable {
    private int _id;
    private string _name;
    private string _rating;
    private static SortOrder _order;

    public enum SortOrder {
        Ascending = 0,
        Descending = 1
    }

    public Employee(int id, string name)
        this(id, name, "Other") {
    }

    public Employee(int id, string name, string rating) {
        this._id = id;
        this._name = name;
        this._rating = rating;
    }

    public int Id {
        get return this._id; }
        set this._id = value; }
    }

    public string Name {
        get return this._name; }
        set this._name = value; }
    }

    public string Rating {
        get return this._rating; }
        set this._rating = value; }
    }

    public static SortOrder Order {
        get return _order; }
        set _order = value; }
    }

    public override bool Equals(Object obj) {
        bool retVal = false;
        if (obj != null) {
            Employee custObj = (Employee)obj;
            if ((custObj.Id == this.Id&&
                (custObj.Name.Equals(this.Name&&
                (custObj.Rating.Equals(this.Rating))))
                retVal = true;
        }
        return retVal;
    }

    public override string ToString() {
        return this._id + ": " this._name;
    }

    public int CompareTo(Object obj) {
        switch (_order) {
            case SortOrder.Ascending:
                return this.Name.CompareTo(((Employee)obj).Name);
            case SortOrder.Descending:
                return (((Employee)obj).Name).CompareTo(this.Name);
            default:
                return this.Name.CompareTo(((Employee)obj).Name);
        }
    }

}

public class CollectionTest {
    public static void Main() {
        List<Employee> collCustList = new List<Employee>();
        collCustList.Add(new Employee(99"H""P"));
        collCustList.Add(new Employee(77"B""G"));
        collCustList.Add(new Employee(55"B""G"));
        collCustList.Add(new Employee(88"B""P"));
        collCustList.Add(new Employee(11"L""O"));

        foreach (Employee cust in collCustList)
            Console.Out.WriteLine(cust);

        Employee.Order = Employee.SortOrder.Ascending;
        collCustList.Sort(delegate(Employee cust1, Employee cust2) {
            return Comparer<Employee>.Default.Compare(cust1, cust2);
        });

        foreach (Employee cust in collCustList)
            Console.Out.WriteLine(cust);

        Employee.Order = Employee.SortOrder.Descending;
        collCustList.Sort(delegate(Employee cust1, Employee cust2) {
            return Comparer<Employee>.Default.Compare(cust1, cust2);
        });

        foreach (Employee cust in collCustList)
            Console.Out.WriteLine(cust);
    }
}

   
  
Related examples in the same category
1.Generic Collection and List
2.Add user-defined object to generic Collection
3.Add Collection Items with IComparable interface implementation
4.Add object in a hierarchy into a generic Collection
5.Generic collection class
6.Represents a generic collection of key/value pairs. The enumerator returns the values in the order assigned.
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.