By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,363 Members | 1,729 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,363 IT Pros & Developers. It's quick & easy.

Sorting objects in ArrayList

P: n/a
Hello
I'm sorry for mistakes - my English isn't very well.

I've got the problem with sorting objects in ArrayList. If I put there only
strings, Sort method works great, but it doesnt work when I put there
objects (even if ToString method is override in object class).
I think that i have to override a comparator in my object class, but even if
it is correc I dont know how to do it...
I'd be very greatfull if Somebody could help me.
Nov 15 '05 #1
Share this Question
Share on Google+
7 Replies


P: n/a
Hi ryba,

Your objects should implement IComparable interface (easy to implement it).

--
Miha Markic - RightHand .NET consulting & software development
miha at rthand com

"ryba" <ba******@poczta.onet.pl> wrote in message
news:br**********@atlantis.news.tpi.pl...
Hello
I'm sorry for mistakes - my English isn't very well.

I've got the problem with sorting objects in ArrayList. If I put there only strings, Sort method works great, but it doesnt work when I put there
objects (even if ToString method is override in object class).
I think that i have to override a comparator in my object class, but even if it is correc I dont know how to do it...
I'd be very greatfull if Somebody could help me.

Nov 15 '05 #2

P: n/a
Miha and ryba,

Or, if you don't want to change the code in the object (or can't), you
can always implement the IComparer interface and then pass that interface to
the Sort method on the ArrayList.

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Miha Markic" <miha at rthand com> wrote in message
news:eG**************@TK2MSFTNGP10.phx.gbl...
Hi ryba,

Your objects should implement IComparable interface (easy to implement it).
--
Miha Markic - RightHand .NET consulting & software development
miha at rthand com

"ryba" <ba******@poczta.onet.pl> wrote in message
news:br**********@atlantis.news.tpi.pl...
Hello
I'm sorry for mistakes - my English isn't very well.

I've got the problem with sorting objects in ArrayList. If I put there only
strings, Sort method works great, but it doesnt work when I put there
objects (even if ToString method is override in object class).
I think that i have to override a comparator in my object class, but

even if
it is correc I dont know how to do it...
I'd be very greatfull if Somebody could help me.


Nov 15 '05 #3

P: n/a
Check out my blog post about this issue:
http://weblogs.asp.net/jan/posts/6479.aspx

I'ts about how to create a generic comparer so you can easily sort objects.

--
Greetz

Jan Tielens
________________________________
Read my weblog: http://weblogs.asp.net/jan
"ryba" <ba******@poczta.onet.pl> wrote in message
news:br**********@atlantis.news.tpi.pl...
Hello
I'm sorry for mistakes - my English isn't very well.

I've got the problem with sorting objects in ArrayList. If I put there only strings, Sort method works great, but it doesnt work when I put there
objects (even if ToString method is override in object class).
I think that i have to override a comparator in my object class, but even if it is correc I dont know how to do it...
I'd be very greatfull if Somebody could help me.

Nov 15 '05 #4

P: n/a
Right, I was just testing if you'll notice that I left out this option ;-)

--
Miha Markic - RightHand .NET consulting & software development
miha at rthand com

"Nicholas Paldino [.NET/C# MVP]" <mv*@spam.guard.caspershouse.com> wrote in
message news:OP**************@TK2MSFTNGP10.phx.gbl...
Miha and ryba,

Or, if you don't want to change the code in the object (or can't), you
can always implement the IComparer interface and then pass that interface to the Sort method on the ArrayList.

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Miha Markic" <miha at rthand com> wrote in message
news:eG**************@TK2MSFTNGP10.phx.gbl...
Hi ryba,

Your objects should implement IComparable interface (easy to implement

it).

--
Miha Markic - RightHand .NET consulting & software development
miha at rthand com

"ryba" <ba******@poczta.onet.pl> wrote in message
news:br**********@atlantis.news.tpi.pl...
Hello
I'm sorry for mistakes - my English isn't very well.

I've got the problem with sorting objects in ArrayList. If I put there

only
strings, Sort method works great, but it doesnt work when I put there
objects (even if ToString method is override in object class).
I think that i have to override a comparator in my object class, but

even
if
it is correc I dont know how to do it...
I'd be very greatfull if Somebody could help me.



Nov 15 '05 #5

P: n/a
Hi,

Please find below a class that you can use to sort a collection of any
type, the sorted type has to implement nothing.

You may use it like this:

ArrayList.Sort( new ClassSorter( "PropertyNameToCompareBy",
SortDirection.Ascending ) );
It's very simple it use reflection to get the value of
PropertyNameToCompareBy which needs to be an IComparable.
A good thing of the shown implementation is that the property may be a
complex type and you can sort by something like:
"TopProperty.InnerProperty"

The class will see that TopProperty has a property and will descend to get
its value.

Hope this help,

--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation

//*********************** Start of the Code ************/

public class ClassSorter: IComparer

{

protected string sortBy;

protected SortDirection sortDirection;
#region Constructors

public ClassSorter(string sortBy, SortDirection

sortDirection)

{

this.sortBy = sortBy;

this.sortDirection = sortDirection;

}

#endregion

int Compare( object x, object y, string comparer)

{

if ( comparer.IndexOf( ".") != -1 )

{

//split the string

string[] parts = comparer.Split( new char[]{ '.'} );

return Compare( x.GetType().GetProperty( parts[0]).GetValue(x, null) ,

y.GetType().GetProperty( parts[0]).GetValue(y, null) , parts[1]

);

}

else

{

IComparable icx, icy;

icx =

(IComparable)x.GetType().GetProperty( comparer).GetValue(x, null);

icy =

(IComparable)y.GetType().GetProperty( comparer).GetValue(y, null);

if ( x.GetType().GetProperty(comparer).PropertyType ==
typeof(System.String) )

{

icx = (IComparable) icx.ToString().ToUpper();

icy = (IComparable) icy.ToString().ToUpper();

}

if(this.sortDirection == SortDirection.Descending)

return icy.CompareTo(icx);

else

return icx.CompareTo(icy);

}

}
public int Compare(object x, object y)

{

return Compare( x, y, sortBy);

}
}

public enum SortDirection

{

Ascending = 0,

Descending = 1

}

//*********************** End of the Code ************/

"ryba" <ba******@poczta.onet.pl> wrote in message
news:br**********@atlantis.news.tpi.pl...
Hello
I'm sorry for mistakes - my English isn't very well.

I've got the problem with sorting objects in ArrayList. If I put there only strings, Sort method works great, but it doesnt work when I put there
objects (even if ToString method is override in object class).
I think that i have to override a comparator in my object class, but even if it is correc I dont know how to do it...
I'd be very greatfull if Somebody could help me.

Nov 15 '05 #6

P: n/a
Hi,

See my other post, I post a piece of code that implement a class that using
reflection let you sort them, all it's required is that the botton property
support IComparable.

Cheers,

--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation

"Miha Markic" <miha at rthand com> wrote in message
news:%2****************@tk2msftngp13.phx.gbl...
Right, I was just testing if you'll notice that I left out this option ;-)

--
Miha Markic - RightHand .NET consulting & software development
miha at rthand com

"Nicholas Paldino [.NET/C# MVP]" <mv*@spam.guard.caspershouse.com> wrote in message news:OP**************@TK2MSFTNGP10.phx.gbl...
Miha and ryba,

Or, if you don't want to change the code in the object (or can't), you can always implement the IComparer interface and then pass that
interface to
the Sort method on the ArrayList.

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Miha Markic" <miha at rthand com> wrote in message
news:eG**************@TK2MSFTNGP10.phx.gbl...
Hi ryba,

Your objects should implement IComparable interface (easy to implement

it).

--
Miha Markic - RightHand .NET consulting & software development
miha at rthand com

"ryba" <ba******@poczta.onet.pl> wrote in message
news:br**********@atlantis.news.tpi.pl...
> Hello
> I'm sorry for mistakes - my English isn't very well.
>
> I've got the problem with sorting objects in ArrayList. If I put there only
> strings, Sort method works great, but it doesnt work when I put there > objects (even if ToString method is override in object class).
> I think that i have to override a comparator in my object class, but

even
if
> it is correc I dont know how to do it...
> I'd be very greatfull if Somebody could help me.
>
>



Nov 15 '05 #7

P: n/a
Thank you all for help...
I've implemented IComaprable interfece (Miha idea).
Nov 15 '05 #8

This discussion thread is closed

Replies have been disabled for this discussion.