T
TC
Hey All,
I recently saw the following on CodeProject:
http://www.codeproject.com/script/Membership/Profiles.aspx?mid=36803
and I have a question regarding Generics and the sorting of objects.
Assuming I have the following classes:
using System;
using System.Collections.Generic;
using System.Text;
namespace JunkTest
{
// Enumeration of sorder order types
public enum SortOrderType
{
Ascending,
Descending
}
// Enumeration of comparison types
public enum PersonComparisonType
{
LastName,
FirstName,
Age
}
public class Person : IComparable<Person>
{
private string _lastName = null;
public string LastName
{
get { return _lastName; }
set { _lastName = value; }
}
private string _firstName = null;
public string FirstName
{
get { return _firstName; }
set { _firstName = value; }
}
private int _age = 0;
public int Age
{
get { return _age; }
set { _age = value; }
}
public Person(string lastName, string firstName, int age)
{
this.LastName = lastName;
this.FirstName = firstName;
this.Age = age;
}
public Person()
{
}
public int CompareTo(Person person)
{
return this.LastName.CompareTo(person.LastName);
}
// Special implementation to be called by custom comparer
public int CompareTo(Person person,
PersonComparisonType comparisonType)
{
switch (comparisonType)
{
case PersonComparisonType.LastName:
return this.LastName.CompareTo(person.LastName);
case PersonComparisonType.FirstName:
return this.FirstName.CompareTo(person.FirstName);
case PersonComparisonType.Age:
return this.Age.CompareTo(person.Age);
}
return 0;
}
}
public class PersonComparer : IComparer<Person>
{
private PersonComparisonType comparisonType =
PersonComparisonType.LastName;
public PersonComparisonType ComparisonType
{
get { return comparisonType; }
set { comparisonType = value; }
}
private SortOrderType sortOrder = SortOrderType.Ascending;
public SortOrderType SortOrder
{
get { return sortOrder; }
set { sortOrder = value; }
}
public PersonComparer(PersonComparisonType comparisonType)
{
this.comparisonType = comparisonType;
}
public bool Equals(Person lhs, Person rhs)
{
return this.Compare(lhs, rhs) == 0;
}
// Tell the Person objects to compare themselves
public int Compare(Person lhs, Person rhs)
{
switch (sortOrder)
{
case SortOrderType.Ascending:
default:
return lhs.CompareTo(rhs, comparisonType);
case SortOrderType.Descending:
return rhs.CompareTo(lhs, comparisonType);
}
}
}
}
I was wondering how one would implement their own QuickSort or BubbleSort
algorithm like you did in the article?
Currently, for example, I do the following:
Person ThisPerson = new Person();
Person ThatPerson = new Person();
Person OtherPerson = new Person();
ThisPerson.Age = 25;
ThisPerson.FirstName = "Tom";
ThisPerson.LastName = "Carr";
MyTotalPersons.Add (ThisPerson);
OtherPerson.Age = 31;
OtherPerson.FirstName = "Kelly";
OtherPerson.LastName = "Britton";
MyTotalPersons.Add(OtherPerson);
ThatPerson.Age = 28;
ThatPerson.FirstName = "James";
ThatPerson.LastName = "Karl";
MyTotalPersons.Add (ThatPerson);
// Sort collection using age
PersonComparer personComparer = new
PersonComparer(PersonComparisonType.LastName);
personComparer.SortOrder = SortOrderType.Descending;
MyTotalPersons.Sort(personComparer);
where 'MyTotalPersons' is declared as follows:
private List<Person> MyTotalPersons=new List<Person>();
The above calls the default built-in sort. How would one implement such
algorithms for objects using Generics?
Thanks,
TC
I recently saw the following on CodeProject:
http://www.codeproject.com/script/Membership/Profiles.aspx?mid=36803
and I have a question regarding Generics and the sorting of objects.
Assuming I have the following classes:
using System;
using System.Collections.Generic;
using System.Text;
namespace JunkTest
{
// Enumeration of sorder order types
public enum SortOrderType
{
Ascending,
Descending
}
// Enumeration of comparison types
public enum PersonComparisonType
{
LastName,
FirstName,
Age
}
public class Person : IComparable<Person>
{
private string _lastName = null;
public string LastName
{
get { return _lastName; }
set { _lastName = value; }
}
private string _firstName = null;
public string FirstName
{
get { return _firstName; }
set { _firstName = value; }
}
private int _age = 0;
public int Age
{
get { return _age; }
set { _age = value; }
}
public Person(string lastName, string firstName, int age)
{
this.LastName = lastName;
this.FirstName = firstName;
this.Age = age;
}
public Person()
{
}
public int CompareTo(Person person)
{
return this.LastName.CompareTo(person.LastName);
}
// Special implementation to be called by custom comparer
public int CompareTo(Person person,
PersonComparisonType comparisonType)
{
switch (comparisonType)
{
case PersonComparisonType.LastName:
return this.LastName.CompareTo(person.LastName);
case PersonComparisonType.FirstName:
return this.FirstName.CompareTo(person.FirstName);
case PersonComparisonType.Age:
return this.Age.CompareTo(person.Age);
}
return 0;
}
}
public class PersonComparer : IComparer<Person>
{
private PersonComparisonType comparisonType =
PersonComparisonType.LastName;
public PersonComparisonType ComparisonType
{
get { return comparisonType; }
set { comparisonType = value; }
}
private SortOrderType sortOrder = SortOrderType.Ascending;
public SortOrderType SortOrder
{
get { return sortOrder; }
set { sortOrder = value; }
}
public PersonComparer(PersonComparisonType comparisonType)
{
this.comparisonType = comparisonType;
}
public bool Equals(Person lhs, Person rhs)
{
return this.Compare(lhs, rhs) == 0;
}
// Tell the Person objects to compare themselves
public int Compare(Person lhs, Person rhs)
{
switch (sortOrder)
{
case SortOrderType.Ascending:
default:
return lhs.CompareTo(rhs, comparisonType);
case SortOrderType.Descending:
return rhs.CompareTo(lhs, comparisonType);
}
}
}
}
I was wondering how one would implement their own QuickSort or BubbleSort
algorithm like you did in the article?
Currently, for example, I do the following:
Person ThisPerson = new Person();
Person ThatPerson = new Person();
Person OtherPerson = new Person();
ThisPerson.Age = 25;
ThisPerson.FirstName = "Tom";
ThisPerson.LastName = "Carr";
MyTotalPersons.Add (ThisPerson);
OtherPerson.Age = 31;
OtherPerson.FirstName = "Kelly";
OtherPerson.LastName = "Britton";
MyTotalPersons.Add(OtherPerson);
ThatPerson.Age = 28;
ThatPerson.FirstName = "James";
ThatPerson.LastName = "Karl";
MyTotalPersons.Add (ThatPerson);
// Sort collection using age
PersonComparer personComparer = new
PersonComparer(PersonComparisonType.LastName);
personComparer.SortOrder = SortOrderType.Descending;
MyTotalPersons.Sort(personComparer);
where 'MyTotalPersons' is declared as follows:
private List<Person> MyTotalPersons=new List<Person>();
The above calls the default built-in sort. How would one implement such
algorithms for objects using Generics?
Thanks,
TC