Click to See Complete Forum and Search --> : Arraylist,IComparer


burledivya
November 1st, 2008, 03:27 PM
Hai Frnds, Iam beginner to dotnet. Please explain mr below in detail.


class User : IComparable {
private string firstName;
private string lastName;

public User(string fn, string ln) {
this.firstName=fn;
this.lastName=ln;
}
//I did not understand this below method at all..please be clear here
public int CompareTo(object obj) {
User u=(User)obj;
if (this.lastName==u.lastName) {
return this.firstName.CompareTo(u.firstName);
} else {
return this.lastName.CompareTo(u.lastName);
}
}

public override string ToString() {
return lastName+" "+firstName;
}

Thanking you in advance

TheCPUWizard
November 1st, 2008, 03:33 PM
//I did not understand this below method at all..please be clear here
public int CompareTo(object obj) {


It simply compares the last and first names.....

What specifically to you not understand???

burledivya
November 1st, 2008, 07:40 PM
Thanks a lot frnd, If you dont mind could you please explain in detail How does IComparer and Icomparable works with arraylist??

TheCPUWizard
November 1st, 2008, 08:04 PM
ICompareble is a simple interface which defines how to compare two objects.

ArrayList (when used with comparable objects), can be sorted by using this interface.

I still dont understand what you are SPECIFICALLY looking for.... :confused: :confused: :confused: