Sort ArrayList of custom objects by String member
arraylist, c#, sorting
Solution
Maybe you should use the extension methods provided in System.Linq namespace:
using System.Linq;
//...
// if you might have objects of other types, OfType<> will
// - filter elements that are not of the given type
// - return an enumeration of the elements already cast to the right type
arrRegion.OfType<Portal.Entidad.Region>().OrderBy(r => r.RegNombre);
// if there is only a single type in your ArrayList, use Cast<>
// to return an enumeration of the elements already cast to the right type
arrRegion.Cast<Portal.Entidad.Region>().OrderBy(r => r.RegNombre);
If you have control over the original ArrayList and you can change its type to a typed list like this `List<Portal.Entidad.Region>`, I would suggest you do it. Then you would not need to cast everything afterward and can sort like this:
var orderedRegions = arrRegion.OrderBy(r => r.RegNombre);
Problem
I'm having a issue sorting an arraylist of custom objects by a string field. This is the code I'm trying to do: ``` arrRegion.Sort(delegate(Portal.Entidad.Region x, Portal.Entidad.Region y) { return x.RegNombre.CompareTo(y.RegNombre); }); ``` But I'm getting this error: ``` Argument type 'anonymous method' is not assignable to parameter type 'System.Collection.IComparer' ``` What am I missing?