sorting an ArrayList with different attributes
-
hi all since i can't describe my problem well, plz check this code, in this code i can sort the list for the x variable -this is fine and working- but wat if i want also to be able to sort the list with y variable as well. i can only use IComparable.Compare with x, i want also to be able to sort the list for y variable how can i do that???? thanks in advance how can i do that using System; using System.Collections; using System.Collections.Generic; public class SamplesArrayList { public class Test : IComparable { public int x; public int y; //this is the variable which i want to sort the list with it also public Test(int x) { this.x = x; } int IComparable.CompareTo(object x) { Test x1 = (Test)x; return (this.x.CompareTo(x1.x)); } } public static void Main() { ArrayList test = new ArrayList(); Test l1 = new Test(7); Test l2 = new Test(3); Test l3 = new Test(9); Test l4 = new Test(2); Test l5 = new Test(4); test.Add(l1); test.Add(l2); test.Add(l3); test.Add(l4); test.Add(l5); test.Sort(); } }
-
hi all since i can't describe my problem well, plz check this code, in this code i can sort the list for the x variable -this is fine and working- but wat if i want also to be able to sort the list with y variable as well. i can only use IComparable.Compare with x, i want also to be able to sort the list for y variable how can i do that???? thanks in advance how can i do that using System; using System.Collections; using System.Collections.Generic; public class SamplesArrayList { public class Test : IComparable { public int x; public int y; //this is the variable which i want to sort the list with it also public Test(int x) { this.x = x; } int IComparable.CompareTo(object x) { Test x1 = (Test)x; return (this.x.CompareTo(x1.x)); } } public static void Main() { ArrayList test = new ArrayList(); Test l1 = new Test(7); Test l2 = new Test(3); Test l3 = new Test(9); Test l4 = new Test(2); Test l5 = new Test(4); test.Add(l1); test.Add(l2); test.Add(l3); test.Add(l4); test.Add(l5); test.Sort(); } }
yosif4444 wrote:
int IComparable.CompareTo(object x) { Test x1 = (Test)x; return (this.x.CompareTo(x1.x)); }
Your compare to can do whatever you like. You return an int, which specifies how far apart two items are, and in what direction. If you want to sort for y within x, then this would work: if (x == x1.x) { return y - x1.y); } else { return x - x1.x; } Or, pass on to the int compareTo, if you want.
Christian Graus - Microsoft MVP - C++ Metal Musings - Rex and my new metal blog
-
yosif4444 wrote:
int IComparable.CompareTo(object x) { Test x1 = (Test)x; return (this.x.CompareTo(x1.x)); }
Your compare to can do whatever you like. You return an int, which specifies how far apart two items are, and in what direction. If you want to sort for y within x, then this would work: if (x == x1.x) { return y - x1.y); } else { return x - x1.x; } Or, pass on to the int compareTo, if you want.
Christian Graus - Microsoft MVP - C++ Metal Musings - Rex and my new metal blog
i think i described my problem wrong i want to sort the list with either x or y not with both of them say object1 x = 5, y = 10 object2 x = 3, y = 7 object3 x = 7, y = 4 Test.Sort() //for x output object2 object1 object3 or Test.Sort() //for y output object3 object2 object1 i hope u got wat i mean thanks
-
i think i described my problem wrong i want to sort the list with either x or y not with both of them say object1 x = 5, y = 10 object2 x = 3, y = 7 object3 x = 7, y = 4 Test.Sort() //for x output object2 object1 object3 or Test.Sort() //for y output object3 object2 object1 i hope u got wat i mean thanks
-
yosif4444 wrote:
i want to sort the list with either x or y not with both of them
So what part of "do whatever you like" don't you understand? :rolleyes:
Christian Graus wrote:
Your compare to can do whatever you like
led mike
I know that i can "do whatever i want" but only in one compare function i want to write many compare functions depending on the variable i want
-
i think i described my problem wrong i want to sort the list with either x or y not with both of them say object1 x = 5, y = 10 object2 x = 3, y = 7 object3 x = 7, y = 4 Test.Sort() //for x output object2 object1 object3 or Test.Sort() //for y output object3 object2 object1 i hope u got wat i mean thanks
OK - I get it. Create an enum for the different sort orders, then use an instance of that to decide what to do in the compare function.
Christian Graus - Microsoft MVP - C++ Metal Musings - Rex and my new metal blog
-
OK - I get it. Create an enum for the different sort orders, then use an instance of that to decide what to do in the compare function.
Christian Graus - Microsoft MVP - C++ Metal Musings - Rex and my new metal blog
Yeah you got my point but can u plz help me with code example, coz i don't know how to implement wat u said thanks in advance
-
Yeah you got my point but can u plz help me with code example, coz i don't know how to implement wat u said thanks in advance
OK, it's pretty simple public class SamplesArrayList { public class Test : IComparable { public enum SortBy { SortByX, SortByY }; private SortBy _sortOrder; public SortBy { get { return _sortOrder; } set { _sortOrder = value; } } public int x; public int y; //this is the variable which i want to sort the list with it also public Test(int x) { this.x = x; } int IComparable.CompareTo(object x) { Test x1 = (Test)x; switch(SortOrder) { int result; case SortBy.SortByX: result = (this.x.CompareTo(x1.x)); break; case SortBy.SortByY: result = (this.y.CompareTo(x1.y)); break; default: throw new ArgumentException("The SortBy enum has been added to, but the CompareTo method has not been updated"); break; } return result; } }
Christian Graus - Microsoft MVP - C++ Metal Musings - Rex and my new metal blog
-
hi all since i can't describe my problem well, plz check this code, in this code i can sort the list for the x variable -this is fine and working- but wat if i want also to be able to sort the list with y variable as well. i can only use IComparable.Compare with x, i want also to be able to sort the list for y variable how can i do that???? thanks in advance how can i do that using System; using System.Collections; using System.Collections.Generic; public class SamplesArrayList { public class Test : IComparable { public int x; public int y; //this is the variable which i want to sort the list with it also public Test(int x) { this.x = x; } int IComparable.CompareTo(object x) { Test x1 = (Test)x; return (this.x.CompareTo(x1.x)); } } public static void Main() { ArrayList test = new ArrayList(); Test l1 = new Test(7); Test l2 = new Test(3); Test l3 = new Test(9); Test l4 = new Test(2); Test l5 = new Test(4); test.Add(l1); test.Add(l2); test.Add(l3); test.Add(l4); test.Add(l5); test.Sort(); } }
this might help ArrayList.Sort Method (IComparer) HTH Russ
-
yosif4444 wrote:
int IComparable.CompareTo(object x) { Test x1 = (Test)x; return (this.x.CompareTo(x1.x)); }
Your compare to can do whatever you like. You return an int, which specifies how far apart two items are, and in what direction. If you want to sort for y within x, then this would work: if (x == x1.x) { return y - x1.y); } else { return x - x1.x; } Or, pass on to the int compareTo, if you want.
Christian Graus - Microsoft MVP - C++ Metal Musings - Rex and my new metal blog
Try it --- using System; using System.Collections; using System.Collections.Generic; public class SamplesArrayList { static char indi; public class Test : IComparable { public int x; public int y; //this is the variable which i want to sort the list with it also public char indi; public Test(int x, int y) { this.x = x; this.y = y; } int IComparable.CompareTo(object x) { if (indi == 'X') { Test x1 = (Test)x; return (this.x.CompareTo(x1.x)); } else { Test y1 = (Test)x; return (this.y.CompareTo(y1.y)); } } } public static void Main() { ArrayList test = new ArrayList(); Test l1 = new Test(7,8); Test l2 = new Test(3,2); Test l3 = new Test(9,1); Test l4 = new Test(2,0); Test l5 = new Test(4,5); indi = 'X'; test.Add(l1); test.Add(l2); test.Add(l3); test.Add(l4); test.Add(l5); test.Sort(); foreach (Test t in test) { Test x1 = t; Console.WriteLine(x1.x+ " "+ x1.y); } Console.ReadKey(); } }
-
Try it --- using System; using System.Collections; using System.Collections.Generic; public class SamplesArrayList { static char indi; public class Test : IComparable { public int x; public int y; //this is the variable which i want to sort the list with it also public char indi; public Test(int x, int y) { this.x = x; this.y = y; } int IComparable.CompareTo(object x) { if (indi == 'X') { Test x1 = (Test)x; return (this.x.CompareTo(x1.x)); } else { Test y1 = (Test)x; return (this.y.CompareTo(y1.y)); } } } public static void Main() { ArrayList test = new ArrayList(); Test l1 = new Test(7,8); Test l2 = new Test(3,2); Test l3 = new Test(9,1); Test l4 = new Test(2,0); Test l5 = new Test(4,5); indi = 'X'; test.Add(l1); test.Add(l2); test.Add(l3); test.Add(l4); test.Add(l5); test.Sort(); foreach (Test t in test) { Test x1 = t; Console.WriteLine(x1.x+ " "+ x1.y); } Console.ReadKey(); } }
using a char is obviously nasty as hell, but this is the general idea, yes.
Christian Graus - Microsoft MVP - C++ Metal Musings - Rex and my new metal blog