473,473 Members | 1,853 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Sorting objects in ArrayList

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
7 1796
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
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
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
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
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
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
Thank you all for help...
I've implemented IComaprable interfece (Miha idea).
Nov 15 '05 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Nick | last post by:
Hi ! I have Objects in my ArrayList These Objects contain a String called "Name". And i want that ArrayList to sort it's objects using the Name element of each object and not something else of...
1
by: Daniel | last post by:
does C# have any collection objects that support sort functionality so that I dont have to write my own sorting algorithm?
6
by: Bryon | last post by:
I need to sort an ArrayList of objects. I am unable to find a method for this. DO I need to role my own sorting? Or is there something like the qsort() function in C of old??? Thanks
2
by: Rob G | last post by:
Hello, I have a basic understanding of Objects, inheritance, polymorhpism, etc. I am new to VB.NET and I am having a problem figuring this out. I want to sort a CheckBoxList. Using the...
19
by: Owen T. Soroke | last post by:
Using VB.NET I have a ListView with several columns. Two columns contain integer values, while the remaining contain string values. I am confused as to how I would provide functionality to...
16
by: RCS | last post by:
So I have an ArrayList that gets populated with objects like: myAL.Add(new CustomObject(parm1,parm2)); I'm consuming this ArrayList from an ObjectDataSource and would like to have this support...
2
by: Rob Meade | last post by:
Dear all, I have a class which contains an arraylist populated with other objects, for example: PrescriptionQueue - containing multiple instances of Prescription I have the need on my web...
6
by: Arthur Dent | last post by:
How do you sort a generic collection derived from System.Collections.ObjectModel.Collection? Thanks in advance, - Arthur Dent
0
by: planb | last post by:
Hello, I am working with a class with a basetype of NameObjectCollection, which has as a member an object array public object Objects {get {return BaseGetAllValues();}} which I want to sort...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.