472,119 Members | 1,723 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,119 software developers and data experts.

HELP can't get arraylist to sort by alphabet

Hi I am new to this language, and still don't understand most of the
concepts, I am trying to sort an arraylist by alphabet, I belive i
need to use the IComparer interface, but have no idea how this works?
Does anyone have a sample of how i can sort my arraylist of words.
Also how is an arraylist iterated over and is it possible to obtain a
count of the number of occurences of each owrd in the array list? Many
thanks.
Nov 16 '05 #1
2 3768
Just call the Sort() method of your ArrayList. It will use the IComparable
implementation of the items in the list, in this case, strings. These
should sort as you expect; you don't have to do Sort(IComparer) unless have
some special requirements.

--Bob

"steve smith" <bo**********@hotmail.com> wrote in message
news:4b**************************@posting.google.c om...
Hi I am new to this language, and still don't understand most of the
concepts, I am trying to sort an arraylist by alphabet, I belive i
need to use the IComparer interface, but have no idea how this works?
Does anyone have a sample of how i can sort my arraylist of words.
Also how is an arraylist iterated over and is it possible to obtain a
count of the number of occurences of each owrd in the array list? Many
thanks.

Nov 16 '05 #2
Steve,

Missed the 2nd parts of your question.

You can iterate over an ArrayList of strings "al" in the following ways:

// for loop

for (int i = 0;i < al.Length;i++) {
string s = (string)al[i];
// do stuff with s
}

// foreach loop

foreach (string s in al) {
// do stuff with s
}

// IEnumerator interface

IEnumerator alEnumerator = al.GetEnumerator();

while (alEnumerator.MoveNext()) {
string s = (string)alEnumerator.Current;
// do stuff with s
}

To obtain a count of occurences you'll have to write your own routine, along
the lines of:

int Occurs(string strSearch,ArrayList al) {
int intCount = 0;

foreach (string s in al) {

if (s == strSearch) {
intCount++;
}

}

return intCount;
}

--Bob

"steve smith" <bo**********@hotmail.com> wrote in message
news:4b**************************@posting.google.c om...
Hi I am new to this language, and still don't understand most of the
concepts, I am trying to sort an arraylist by alphabet, I belive i
need to use the IComparer interface, but have no idea how this works?
Does anyone have a sample of how i can sort my arraylist of words.
Also how is an arraylist iterated over and is it possible to obtain a
count of the number of occurences of each owrd in the array list? Many
thanks.

Nov 16 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

2 posts views Thread by D. Roshani | last post: by
4 posts views Thread by jarkkotv | last post: by
4 posts views Thread by Bob Weiner | last post: by
1 post views Thread by almurph | last post: by

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.