TECHNOLOGIES
FORUMS
JOBS
BOOKS
EVENTS
INTERVIEWS
Live
MORE
LEARN
Training
CAREER
MEMBERS
VIDEOS
NEWS
BLOGS
Sign Up
Login
No unread comment.
View All Comments
No unread message.
View All Messages
No unread notification.
View All Notifications
Answers
Post
An Article
A Blog
A News
A Video
An EBook
An Interview Question
Ask Question
Forums
Monthly Leaders
Forum guidelines
Maha
NA
0
328.3k
Sort Order
Aug 31 2012 9:32 AM
In this program what does this mean (problem is highlighted)
using System;
using System.Collections;
public class Person : IComparable
{
public enum SortMethod
{ Firstname = 0, Lastname = 1, Age = 2 };
private string firstname;
private string lastname;
private int age;
private static SortMethod sortOrder;
public string Firstname
{
get { return firstname; }
set { firstname = value; }
}
public string Lastname
{
get { return lastname; }
set { lastname = value; }
}
public int Age
{
get { return age; }
set { age = value; }
}
public static SortMethod SortOrder
{
get { return sortOrder; }
set { sortOrder = value; }
}
public Person(string firstname, string lastname, int age)
{
this.firstname = firstname;
this.lastname = lastname;
this.age = age;
}
public override string ToString()
{
return String.Format("{0} {1}, Age = {2}", firstname, lastname, age.ToString());
}
public int CompareTo(object obj)
{
if (obj is Person)
{
Person p2 = (Person)obj;
switch (sortOrder)
{
case SortMethod.Lastname:
return lastname.CompareTo(p2.Lastname);
case SortMethod.Age:
return age.CompareTo(p2.Age);
case SortMethod.Firstname:
default:
return firstname.CompareTo(p2.Firstname);
}
}
else
throw new ArgumentException("Object is not a Person.");
}
}
public class TestClass
{
public static void Main()
{
ArrayList people = new ArrayList();
people.Add(new Person("John", "Doe", 76));
people.Add(new Person("Abby", "Normal", 25));
people.Add(new Person("Jane", "Doe", 84));
people.Sort();
foreach (Person p in people)
Console.WriteLine(p);
Console.ReadLine();
Person.SortOrder = Person.SortMethod.Lastname;
people.Sort();
foreach (Person p in people)
Console.WriteLine(p);
Console.ReadLine();
Person.SortOrder = Person.SortMethod.Age;
people.Sort(); //not Array.Sort(people)
foreach (Person p in people)
Console.WriteLine(p);
Console.ReadLine();
}
}
/*
Abby Normal, Age = 25
Jane Doe, Age = 84
John Doe, Age = 76
John Doe, Age = 76
Jane Doe, Age = 84
Abby Normal, Age = 25
Abby Normal, Age = 25
John Doe, Age = 76
Jane Doe, Age = 84
*/
Reply
Answers (
6
)
What a strange behavior in AuotComplete in DataGridViewCombobox Column ?
how to play swf file from ms access database in c# windows form