Use SortedDictionary to store class implementing System.IComparable : SortedDictionary « Collections Data Structure « 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.Date Time
8.Design Patterns
9.Development Class
10.Event
11.File Stream
12.Generics
13.GUI Windows Form
14.Internationalization I18N
15.Language Basics
16.LINQ
17.Network
18.Office
19.Reflection
20.Regular Expressions
21.Security
22.Services Event
23.Thread
24.Web Services
25.Windows
26.Windows Presentation Foundation
27.XML
28.XML LINQ
C# / C Sharp » Collections Data Structure » SortedDictionaryScreenshots 
Use SortedDictionary to store class implementing System.IComparable
 

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


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

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

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

    public Customer(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) {
            Customer custObj = (Customer)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(((Customer)obj).Name);
            case SortOrder.Descending:
                return (((Customer)obj).Name).CompareTo(this.Name);
            default:
                return this.Name.CompareTo(((Customer)obj).Name);
        }
    }
}

class Program {
    public static void Main(){
        Dictionary<int, Customer> custDict = new Dictionary<int, Customer>();
        custDict[99new Customer(99"H""P");
        custDict[77new Customer(77"B""G");
        custDict[55new Customer(55"B""S");
        custDict[88new Customer(88"B""P");

        Dictionary<int, Customer>.ValueCollection unsortedValues;
        unsortedValues = custDict.Values;
        foreach (Customer cust in unsortedValues)
            Console.Out.WriteLine("Customer Name: {0}", cust.Name);

        SortedDictionary<int, Customer> sortedDict;
        sortedDict = new SortedDictionary<int, Customer>(custDict);

        SortedDictionary<int, Customer>.ValueCollection sortedValues;
        sortedValues = sortedDict.Values;
        foreach (Customer cust in sortedValues) {
            Console.Out.WriteLine("Customer->{0}", cust.Name);
        }

        sortedDict.Remove(88);

        sortedValues = sortedDict.Values;
        foreach (Customer cust in sortedValues) {
            Console.Out.WriteLine("Customer->{0}", cust.Name);
        }
    }
}

 
Related examples in the same category
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.