473,387 Members | 1,504 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 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 3836
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: D. Roshani | last post by:
Hello ! I wonder if any one can help me to create a cosomize sorting order (as Macro or added small program in c++ or c# which does this work) in a Access Database contaning one table only words...
4
by: jarkkotv | last post by:
Hi everyone! I'm having a little problem when sorting the ArrayList and I was wondering if there is a .NET guru who can help me out :) I'm trying to sort ArrayList alphabetically in ASP.Net...
4
by: Bob Weiner | last post by:
What I want to be able to do is create an indexer that can index into an ArrayList filled with objects of my own type. I have the following class structure: ...
11
by: steve smith | last post by:
Hi I'm still having some problems getting my head round this language. A couple of things don't seem to work for me. First I am trying to obtan a count of the number of words in a sting, so am...
8
by: Jack Addington | last post by:
I want to scroll through the alphabet in order to scroll some data to the closest name that starts with a letter. If the user hits the H button then it should scroll to the letter closest to H. ...
3
by: jtfaulk | last post by:
Re: ArrayList, Sort, Menu, IComparer, Object, multidemensional I have a multi-dimensional arraylist, and I would like to sort one level of it but not all. The multi-dimensional arraylist...
12
by: Justin | last post by:
Ok, I give up. I can't seem to construct a decent (productive) way of sorting my arraylist. I have a structure of two elements: Structure TabStructure Dim TabName As String Dim FullFilePath...
2
by: almurph | last post by:
H ieveryone, Can you help me please? I am trying to sort a hashtable but get the error: "Cannot implicity convert type void to System.Collections.ArrayList" I am doing the following: ...
1
by: almurph | last post by:
H ieveryone, Can you help me please? I am trying to sort a hashtable but get the error: "Cannot implicity convert type void to System.Collections.ArrayList" I am doing the following: ...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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,...

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.