sorting a string collection

  • Thread starter Thread starter Martin
  • Start date Start date
M

Martin

Hi,

Can anybody please tell me if it is possible to sort a string collection
into alphabetical order (or indeed sort in any order at all).
I have made a "stringCollection" which is in the
"System.Collections.Specialized" namespace, however there does not seem to
be any "sort" method.

my code is below

System.Collections.Specialized.StringCollection stringCol = new
System.Collections.Specialized.StringCollection();
stringCol.Add("martin");
stringCol.Add("George");
stringCol.Add("Adrian");
stringCol.Add("Charlie");
stringCol.Add("Greg");
stringCol.Add("Albert");

stringCol.sort /// Can't be this......

Please can somebody let me know the best way to implement a sort method --
maybe I have to derive my own....

thanks in advance

cheers

martin.
 
Thanks brock,

does this mean that I will have to use the "Array" class instead of
"stringcollection" class, as the array.sort method can not take a
stringcollection object.

does this also mean that I will have to write my own IComparer interface to
compare my string "objects"

I was hoping to use code simialr to below, but I guess I may have to write
my own compare method.

cheers

martin.

System.Collections.Specialized.StringCollection stringCollection = new
StringCollection();
stringCollection.Add("eeee");
stringCollection.Add("wwwwvv");
stringCollection.Add("vvv");
stringCollection.Add("xxxx");
stringCollection.Add("bbbb");
stringCollection.Add("zzzzz");
Array.Sort(stringCollection);
 
Yes, you will have to use an array.

You dont need to implement IComparer iIf all you need is string sorting. See
Comparer and CaseInsensitiveComparer

-Atul
http://www.ssware.com/
Shell MegaPack - Windows Explorer Shell Controls for ActiveX and .Net
 
An alternative solution is to use the Adapter method of the ArrayList class.
This allows you to use ArrayList functionality on any object which
implements the IList method (which StringCollection supports). This will
then enable you to use the ArrayList's sort method.

Hope this helps,

Nick Hall
 
Back
Top