/// <summary>
/// A generic bubblesort method implementation
/// Bubblesort Explanation:
http://en.wikipedia.org/wiki/Bubblesort
/// </summary>
/// <typeparam name="T">The object type populating the
list</typeparam>
/// <param name="sourceList">A list of objects</param>
/// <param name="customComparer">A custom comparer object used to
compare the list items</param>
public static void BubbleSort<T>(IList<T> sourceList, IComparer<T>
customComparer)
{
try
{
// Ensure that no null values have been provided
if (sourceList == null)
{
throw new ArgumentNullException("sourceList",
Properties.Resources.NullParam);
}
else if (customComparer == null)
{
throw new ArgumentNullException("customComparer",
Properties.Resources.NullParam);
}
for (int counter = sourceList.Count - 1; counter >= 0;
counter--)
{
// The inner loop ensures that all values are
re-compared and swapped, if necessary
for (int index = 1; index <= counter; index++)
{
if (customComparer.Compare(sourceList[index - 1],
sourceList[index]) > 0)
{
// Create a temporary holding object and swap
the order of the items
T tempListItem = sourceList[index - 1];
sourceList[index - 1] = sourceList[index];
sourceList[index] = tempListItem;
}
}
}
}
catch (System.Exception exception)
{
ExceptionHandler.HandleException(exception);
}
}
/// <summary>
/// A generic quicksort method implementation
/// </summary>
/// <typeparam name="T">The object type populating the
list</typeparam>
/// <param name="sourceList">A list of objects</param>
/// <param name="customComparer">A custom comparer object used to
compare the list items</param>
public static void QuickSort<T>(IList<T> sourceList, IComparer<T>
customComparer)
{
try
{
// Ensure that no null values have been provided
if (sourceList == null)
{
throw new ArgumentNullException("sourceList",
Properties.Resources.NullParam);
}
else if (customComparer == null)
{
throw new ArgumentNullException("customComparer",
Properties.Resources.NullParam);
}
// Call the actual quicksort algorithm
CustomObjectListSort.PrivateQuickSort(sourceList,
customComparer, 0, sourceList.Count - 1);
}
catch (System.Exception exception)
{
ExceptionHandler.HandleException(exception);
}
}
#endregion
#region Private Methods
/// <summary>
/// A generic quicksort algorithm implementation
/// Quicksort Explanation:
http://en.wikipedia.org/wiki/Quicksort
/// </summary>
/// <typeparam name="T">The object type populating the
list</typeparam>
/// <param name="sourceList">A list of objects</param>
/// <param name="customComparer">A custom comparer object used to
compare the list items</param>
/// <param name="lowIndex">Lower index in the list</param>
/// <param name="highIndex">Upper index in the list</param>
private static void PrivateQuickSort<T>(IList<T> sourceList,
IComparer<T> customComparer,int lowIndex, int highIndex)
{
try
{
// Ensure that no null values have been provided
if (sourceList == null)
{
throw new ArgumentNullException("sourceList",
Properties.Resources.NullParam);
}
else if (customComparer == null)
{
throw new ArgumentNullException("customComparer",
Properties.Resources.NullParam);
}
int tempLowIndex = lowIndex + 1;
int tempHighIndex = highIndex;
// If the lowIndex is not less than the highIndex, the sort
is complete
if (lowIndex < highIndex)
{
// Create a temporary holding object for swapping the
order of the items
T tempListItem = sourceList[lowIndex];
// Loop through list until all items
// between indices have been compared
while (tempLowIndex < tempHighIndex)
{
// Keep incrementing temp indices until appropriate
discrepancy for swap is found
if (customComparer.Compare(sourceList[tempLowIndex],
tempListItem) <= 0)
{
tempLowIndex++;
}
else if
(customComparer.Compare(sourceList[tempHighIndex], tempListItem) >= 0)
{
tempHighIndex--;
}
else
{
tempListItem = sourceList[tempLowIndex];
sourceList[tempLowIndex] =
sourceList[tempHighIndex];
sourceList[tempHighIndex] = tempListItem;
}
}
if (customComparer.Compare(sourceList[tempLowIndex],
tempListItem) < 0)
{
// If tempLow is less than lowIndex, swap the items
tempListItem = sourceList[tempLowIndex];
sourceList[tempLowIndex] = sourceList[lowIndex];
sourceList[lowIndex] = tempListItem;
tempLowIndex--;
}
else
{
// Otherwise, swap item just below tempLow with the
lowIndex
tempLowIndex--;
tempListItem = sourceList[tempLowIndex];
sourceList[tempLowIndex] = sourceList[lowIndex];
sourceList[lowIndex] = tempListItem;
}
// Recursively call the quicksort function until both
partitions
// (i.e. 'lowIndex' and 'highIndex' pieces of list) are
complete
PrivateQuickSort(sourceList, customComparer, lowIndex,
tempLowIndex);
PrivateQuickSort(sourceList, customComparer,
tempHighIndex, highIndex);
}
}
catch (System.Exception exception)
{
ExceptionHandler.HandleException(exception);
}
}
TC said:
Hey All,
I've been handed a brain teaser of sorts and I'm hoping that you may be of
assistance.
Using a List or such of Generics objects, what would be the best way of
constructing one's own sorting algorithm?
For example, say we have class 'Dog' which has the properties 'Color',
'Age' and 'Name'. How would you go about building a generic sorting
algorithm such that one could sort by any of the above properties?
Please keep in mind that using .Net's native sorting method functions
would not be allowed.
I'm trying to think of a way to build a simple bubble sort but how to make
it transparent such that a consumer of the class or algorithm can specify
the sort key for the objects based upon the object's properties?
Thanks,
TC