Hi guys,
I'm trying to compare some classes and list them in order based on multiple attributes. This is a bit beyond me, so I went online to look for a public domain algorithm to do it. I believe I found something like what I'm looking for here:
http://www.codeproject.com/KB/recipe...h_Objects.aspx
Here's what I'm trying to do: Say I have the following class:
Code:
class GenericClass
GenericClass()
{
this.DoubleComponents = DoubleComponents;
}
double[] DoubleComponenets {get; set;}
}
And I initialize it the following way (pseudocode)
Code:
int NumComponents = 3 // could be 1 - infinity, but 3 is standard
List<GenericClass> ToSort = PopulateList(NumComponents);
///
public static List<GenericClass> PopulateList(int NumComponents)
{
int[] AddToList = new int[NumComponents] /// ... and then add to list, etc
}
///
int[] ColumnsToSortBy = GetColumnsFromString(Console.ReadLine());
Array.Sort (//somehow sort ToSort by ColumnsToSortBy)
So say my ToSort is made up of the following:
{1 4 5}
{1 2 5}
{1 2 2}
{2 5 6}
{5 3 1}
{6 1 6}
If the user enters "2 3 1" for GetColumnsFromString, I want to sort the list by columns 2 and 3 (I know, convert it to 1 and 2 to get the index of the component of the array) so that it looks as follows:
{6 1 6}
{1 2 2}
{1 2 5}
{5 3 1}
{1 4 5}
{2 5 6}
Likewise, if the user enters "1 3 2"
I want the list to appear as
{1 2 2}
{1 2 5}
{1 4 5}
{2 5 6}
{5 3 1}
{6 1 6}
So in general, the list is sorted by column in the order that the user enters the columns.
I tried to make the following modification to the code on the web site:
Code:
public int Compare(ComparableObject x, ComparableObject y, int[] index)
            {
                Type t = x.GetType();
                object[] indexargs = new object[index.Length];
                for (int z = 0; z < index.Length; z++) { indexargs[z] = index[z]; }
// further into the code
int iResult = Comparer.DefaultInvariant.Compare(val.GetValue(x, indexargs), val.GetValue(y, indexargs));
However, I'm not sure how to pass the index[] parameter into the function, since the function that's actually called from the Main() function is ObjectComparer<Objects.Peak>("ChemicalShifts", true). Can somebody please advise?

P.S. Sorry for any formatting errors... this is from my phone.