Click here to Skip to main content
15,885,767 members
Home / Discussions / C#
   

C#

 
AnswerRe: process details Pin
Luc Pattyn2-Mar-09 3:25
sitebuilderLuc Pattyn2-Mar-09 3:25 
QuestionDrag and drop to mouse point location Pin
Low Pei Yang2-Mar-09 0:03
Low Pei Yang2-Mar-09 0:03 
AnswerRe: Drag and drop to mouse point location Pin
Henry Minute2-Mar-09 10:51
Henry Minute2-Mar-09 10:51 
GeneralRe: Drag and drop to mouse point location Pin
Low Pei Yang2-Mar-09 16:13
Low Pei Yang2-Mar-09 16:13 
QuestionIComparer problem Pin
livez1-Mar-09 23:22
livez1-Mar-09 23:22 
AnswerRe: IComparer problem Pin
CPallini1-Mar-09 23:42
mveCPallini1-Mar-09 23:42 
AnswerRe: IComparer problem Pin
DaveyM691-Mar-09 23:47
professionalDaveyM691-Mar-09 23:47 
AnswerRe: IComparer problem PinPopular
J4amieC2-Mar-09 0:13
J4amieC2-Mar-09 0:13 
I wrote you a little sameple which might help.

I have 3 types of pet:

public class Dog { }
public class Cat { }
public class Rabbit { }


I dont want to order these alphabetically, I want to order them in order of my preference of them as pets. I like cats more than dogs, and dogs more than rabbits. I could write a comparer to always order a list of pets in my prefered order:

public class MyPetPreferenceComparer : IComparer
{
    #region IComparer Members

    public int Compare(object x, object y)
    {
        // if they are same type return 0
        if (x.GetType() == y.GetType())
            return 0;

        // I like cats best so if lhs is a cat it comes before whatever is rhs
        if (x is Cat)
        {
            return -1;
        }
                    
        if(x is Dog)
        {
            // lhs is a dog, if rhs is a rabbit dog comes before
            if (y is Rabbit)
                return -1;
            // otherwise dog comes after
            return 1;
        }
        // I like rabbits least so they always come after
        if (x is Rabbit)
            return 1;

        return 0;
    }

    #endregion
}


Now the test code:

ArrayList list = new ArrayList();
list.Add(new Rabbit());
list.Add(new Cat());
list.Add(new Dog());
list.Add(new Cat());
list.Add(new Rabbit());
list.Add(new Dog());

list.Sort(new MyPetPreferenceComparer());
foreach (object obj in list)
{
    Console.WriteLine(obj.GetType().Name);
}


Outputs:
Cat
Cat
Dog
Dog
Rabbit
Rabbit


One thing Id add - nowdays we tend to prefer the generic versions of these sorts of interface so try to use IComparer<T> in System.Collection.Generics
GeneralRe: IComparer problem Pin
livez2-Mar-09 0:37
livez2-Mar-09 0:37 
AnswerRe: IComparer problem Pin
Luc Pattyn2-Mar-09 3:28
sitebuilderLuc Pattyn2-Mar-09 3:28 
QuestionC# Code Pin
Zeyad Jalil1-Mar-09 23:03
professionalZeyad Jalil1-Mar-09 23:03 
Answerrepost - Ignore Pin
J4amieC1-Mar-09 23:21
J4amieC1-Mar-09 23:21 
Questionexceptions Pin
aratireddy1-Mar-09 23:02
aratireddy1-Mar-09 23:02 
AnswerRe: exceptions Pin
Calin Tatar1-Mar-09 23:15
Calin Tatar1-Mar-09 23:15 
AnswerRe: exceptions Pin
N a v a n e e t h1-Mar-09 23:16
N a v a n e e t h1-Mar-09 23:16 
Answerrepost - ignore Pin
J4amieC1-Mar-09 23:22
J4amieC1-Mar-09 23:22 
Questionhow to add the subtitles in video Pin
Abhishek Rana1-Mar-09 23:02
Abhishek Rana1-Mar-09 23:02 
AnswerRe: how to add the subtitles in video Pin
Eddy Vluggen2-Mar-09 0:02
professionalEddy Vluggen2-Mar-09 0:02 
QuestionFind a file path with seatching it name Pin
abbd1-Mar-09 22:49
abbd1-Mar-09 22:49 
AnswerRe: Find a file path with seatching it name Pin
Calin Tatar1-Mar-09 22:52
Calin Tatar1-Mar-09 22:52 
QuestionRe: Find a file path with seatching it name Pin
abbd1-Mar-09 23:02
abbd1-Mar-09 23:02 
AnswerRe: Find a file path with seatching it name Pin
Alan N2-Mar-09 0:40
Alan N2-Mar-09 0:40 
AnswerRe: Find a file path with seatching it name [modified] Pin
Luc Pattyn2-Mar-09 3:35
sitebuilderLuc Pattyn2-Mar-09 3:35 
QuestionHow to create a Word Template folder Pin
Tejabhiram1-Mar-09 22:45
Tejabhiram1-Mar-09 22:45 
QuestionTo load gridview when scrolling. [modified] Pin
Elango N1-Mar-09 22:35
Elango N1-Mar-09 22:35 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.