How to sort arraylist ?
-
Hello! I have a ArrayList, that contains class (for example):
class A { int a int b public A() { a=0; b=0; } public A(int Na, int Nb) { a=Na; b=Nb; } }
How to sort this elements in the arraylist ? The sorting field is A.One nation - underground
-
Hello! I have a ArrayList, that contains class (for example):
class A { int a int b public A() { a=0; b=0; } public A(int Na, int Nb) { a=Na; b=Nb; } }
How to sort this elements in the arraylist ? The sorting field is A.One nation - underground
If you're using .NET 2.0, use a List instead and provide a sorting function. I can't recall, but I thought you can do this in 1.1 and 1.0 as well, probably just with lots of casting of objects.
Christian Graus - Microsoft MVP - C++ Metal Musings - Rex and my new metal blog "I am working on a project that will convert a FORTRAN code to corresponding C++ code.I am not aware of FORTRAN syntax" ( spotted in the C++/CLI forum )
-
Hello! I have a ArrayList, that contains class (for example):
class A { int a int b public A() { a=0; b=0; } public A(int Na, int Nb) { a=Na; b=Nb; } }
How to sort this elements in the arraylist ? The sorting field is A.One nation - underground
-
Hello! I have a ArrayList, that contains class (for example):
class A { int a int b public A() { a=0; b=0; } public A(int Na, int Nb) { a=Na; b=Nb; } }
How to sort this elements in the arraylist ? The sorting field is A.One nation - underground
As others said, if you are using .NET 2.0 use generic collection (
List<A>
). As for sorting, you probably mean sorting by field int 'a'? As you dont expose anything (a,b is private) you could implementIComparable
. Or if you can expose sorting field, custom implementation ofIComparer
that knows how to sort class A (e.g. A.SortByFieldAComparer). Look up appropriate interfaces in MSDN or google.
"Throughout human history, we have been dependent on machines to survive. Fate, it seems, is not without a sense of irony. " - Morpheus "Real men use mspaint for writing code and notepad for designing graphics." - Anna-Jayne Metcalfe
-
If you're using .NET 2.0, use a List instead and provide a sorting function. I can't recall, but I thought you can do this in 1.1 and 1.0 as well, probably just with lots of casting of objects.
Christian Graus - Microsoft MVP - C++ Metal Musings - Rex and my new metal blog "I am working on a project that will convert a FORTRAN code to corresponding C++ code.I am not aware of FORTRAN syntax" ( spotted in the C++/CLI forum )
Christian Graus wrote:
I thought you can do this in 1.1 and 1.0 as well, probably just with lots of casting of objects.
Yup, for sorting operation there must be operators <, >,= defined (or some of them at least), which System.object does not have. :)
"Throughout human history, we have been dependent on machines to survive. Fate, it seems, is not without a sense of irony. " - Morpheus "Real men use mspaint for writing code and notepad for designing graphics." - Anna-Jayne Metcalfe
-
As others said, if you are using .NET 2.0 use generic collection (
List<A>
). As for sorting, you probably mean sorting by field int 'a'? As you dont expose anything (a,b is private) you could implementIComparable
. Or if you can expose sorting field, custom implementation ofIComparer
that knows how to sort class A (e.g. A.SortByFieldAComparer). Look up appropriate interfaces in MSDN or google.
"Throughout human history, we have been dependent on machines to survive. Fate, it seems, is not without a sense of irony. " - Morpheus "Real men use mspaint for writing code and notepad for designing graphics." - Anna-Jayne Metcalfe
Hello, Just because I'm curious: Do you know of a equivalent Comparer class to System.Collection.CaseInsensitiveComparer, which comparse int values? Or is the solution to use "ToString" for comparison.
dnh wrote:
As for sorting, you probably mean sorting by field int 'a'?
I'm happy that it's not only me, who thinks that the question is not valid. All the best, Martin
-
Hello, Just because I'm curious: Do you know of a equivalent Comparer class to System.Collection.CaseInsensitiveComparer, which comparse int values? Or is the solution to use "ToString" for comparison.
dnh wrote:
As for sorting, you probably mean sorting by field int 'a'?
I'm happy that it's not only me, who thinks that the question is not valid. All the best, Martin
Hmm, there is only one way how to compare integers (that makes sense) as far as I know.
System.Int32
implementsIComparable
, so no need for comparer.
"Throughout human history, we have been dependent on machines to survive. Fate, it seems, is not without a sense of irony. " - Morpheus "Real men use mspaint for writing code and notepad for designing graphics." - Anna-Jayne Metcalfe
-
Hmm, there is only one way how to compare integers (that makes sense) as far as I know.
System.Int32
implementsIComparable
, so no need for comparer.
"Throughout human history, we have been dependent on machines to survive. Fate, it seems, is not without a sense of irony. " - Morpheus "Real men use mspaint for writing code and notepad for designing graphics." - Anna-Jayne Metcalfe
Hello dnh, 2 minutes after I posted this yesterday, I hoped you would ignore it. :rolleyes: So the .Net 1.1 solution for me would be, use the Sort method of the ArrayList class and give a user specific IComparer instance as parameter. Userspecific IComparer instance would be something like this:
public class ComparerForIntMemberaInClassA : IComparer
{
int System.Collections.IComparer.Compare( object x, object y )
{
A firstA = x as A;
A secondA = y as A;
return firstA.a.CompareTo(secondA.a);
}
}What's your feeling about it? Thank you for you interest! All the best, Martin
-
Hello dnh, 2 minutes after I posted this yesterday, I hoped you would ignore it. :rolleyes: So the .Net 1.1 solution for me would be, use the Sort method of the ArrayList class and give a user specific IComparer instance as parameter. Userspecific IComparer instance would be something like this:
public class ComparerForIntMemberaInClassA : IComparer
{
int System.Collections.IComparer.Compare( object x, object y )
{
A firstA = x as A;
A secondA = y as A;
return firstA.a.CompareTo(secondA.a);
}
}What's your feeling about it? Thank you for you interest! All the best, Martin
yes, that looks fine. Other solution would be implement IComparable in class A; I don't know the guidelines but to me it feels like for "default" sorting I would implement IComparable in class I want to sort (if it is class I own - if it is e.g .NET framework class then I can't do that) and non standard comparations (e.g case insensitive) would go into custom comparers. If you are sorting by private members, nested class should solve access problem.
"Throughout human history, we have been dependent on machines to survive. Fate, it seems, is not without a sense of irony. " - Morpheus "Real men use mspaint for writing code and notepad for designing graphics." - Anna-Jayne Metcalfe
-
yes, that looks fine. Other solution would be implement IComparable in class A; I don't know the guidelines but to me it feels like for "default" sorting I would implement IComparable in class I want to sort (if it is class I own - if it is e.g .NET framework class then I can't do that) and non standard comparations (e.g case insensitive) would go into custom comparers. If you are sorting by private members, nested class should solve access problem.
"Throughout human history, we have been dependent on machines to survive. Fate, it seems, is not without a sense of irony. " - Morpheus "Real men use mspaint for writing code and notepad for designing graphics." - Anna-Jayne Metcalfe