need help on sorting a list of objects

I have my custom class like so. Then I do this:

List<myclass> mylist= new List<myclass>();

now I have this right now so it sorts on the times and that works fine and great.

[0,0,0,1,0,1,0,1,2,2,1,2]= times
[1,2,3,4,5,6,7,8,9,10,11]=index

[0,0,0,0,0,1,1,1,1,2,2,2]= sorted times
[1,2,3,5,7,4,6,8,10,9,10,11]= indexes when time is sorted

My problem is I need those indexes to be sorted also so say any myclass with time of 0 is then sorted in order of index smallest to greatest.

Ive been looking at sorting tutorials for along time but I cant get any of them to work. Can anyone help me?

[code]
public class myclass : Icomparable
{
int time, int index;

public Int32 CompareTo(object o)
{
myclass t = (myclass)o;
return (this.time.CompareTo(t.time));
}
}
[/code]

[840 byte] By [abcdefgqwerty2] at [2008-1-6]
# 1
The problem is you are using List.Sort() which uses the default comparer for your type which is the CompareTo method you have already implemented. This only supports one type of comparison, but you need to support two. Thus, you need to create a new class which implements the Generic IComparer interface. You can then use this class by calling List.Sort(Generic IComparer).
ShellShock at 2007-10-2 > top of Msdn Tech,.NET Development,.NET Base Class Library...
# 2
Thus, you need to create a new class which implements the Generic IComparer interface. You can then use this class by calling List.Sort(Generic IComparer).

Thanks a lot for the fast reply your right im using the default comparer i guess, but how can I implement that interface im not sure how to start. i guess i need more help or an example of just how to start.

I see the interface method looks like this:
public int Compare(myclass x, myclass y)
{}
how would I call sort()? and then this compare() method runs for every 2 classes in my list? maybe i just dont understand how this works.

abcdefgqwerty2 at 2007-10-2 > top of Msdn Tech,.NET Development,.NET Base Class Library...
# 3

Something like this:

Code Snippet
public class MyClass : IComparable
{
private int _time;
private int _index;
public int Index
{
get { return _index; }
set { _index = value; }
}
public MyClass(int index)
{
_index = index;
}
public int CompareTo(object obj)
{
return this._time.CompareTo(((MyClass)obj)._time);
}
}
public class MyComparer : IComparer<MyClass>
{
public int Compare(MyClass x, MyClass y)
{
return x.Index.CompareTo(y.Index);
}
}

class Program
{
static void Main(string[] args)
{
List<MyClass> list = new List<MyClass>();
list.Add(new MyClass(3));
list.Add(new MyClass(1));
list.Add(new MyClass(2));
list.Sort(new MyComparer());
foreach (MyClass myClass in list)
{
Console.WriteLine(myClass.Index.ToString());
}
}
}

ShellShock at 2007-10-2 > top of Msdn Tech,.NET Development,.NET Base Class Library...
# 4
You are the man Thanks for all your help and effort in doing that it helps a lot. Thats exactly what i needed or just some simple example where I can understand whats going on without tons of code to read. Im not the best with defining or even understanding what a comparer exactly is or does. Nor do I understand when they fire.

I see I thought of trying something like that, how does it know to fire both comparer objects if they are in different classes? ah i see because the mycomparer is extending the original myclass which does the time compare. It seems like kind of a pain to do for complex sorting.

abcdefgqwerty2 at 2007-10-2 > top of Msdn Tech,.NET Development,.NET Base Class Library...
# 5
It might seem a bit of extra work but the beauty of it is that the List.Sort method can embody the sorting algorithm without knowing anything about the types it is comparing. This makes it very extensible - it can be used to sort a list of anything.
ShellShock at 2007-10-2 > top of Msdn Tech,.NET Development,.NET Base Class Library...

.NET Development

Site Classified