Sorting objects in ArrayList

R

ryba

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.
 
M

Miha Markic

Hi ryba,

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

Nicholas Paldino [.NET/C# MVP]

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.
 
M

Miha Markic

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 said:
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]
- (e-mail address removed)



Miha Markic said:
Hi ryba,

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

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

even
 
I

Ignacio Machin \( .NET/ C# MVP \)

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 ************/
 
I

Ignacio Machin \( .NET/ C# MVP \)

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 said:
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

message news:[email protected]...
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]
- (e-mail address removed)



Miha Markic said:
Hi ryba,

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

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

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Top