Help sorting an array

Hello,

I have an array of names that is sorted by last name. There are some names like "John Doe" and "Jason von Doe"

What I want to do is add letters of the alphabet (A-Z) above the first instance of the last name. For example, in the array the listing will look like this:

John Carter
D
Jason von Doe
John Doe
E
Wyatt Earp

I realize I will need to create a new array with the updated format. I'm just looking for the algorithm to meet my needs.

LVL 1
InquisitiveProgrammerAsked:
Who is Participating?
 
InquisitiveProgrammerConnect With a Mentor Author Commented:
I finally got it coded:

 
char letter = 'A';
        boolean letterAdded = false;
        
        for (int i = 0; i < searchNames.size(); i++) {
        	
        	// Name in searchNames array
        	String searchString = searchNames.get(i).toString();
        	
        	// Store last name; our searchName fields contain one space between first and last name.
        	// We want to analyze the first letter of the last name.
        	int searchPattern = searchString.lastIndexOf(" ");
        	String resultingString = searchString.substring(searchPattern + 1);
        	
        	
        	if (resultingString.indexOf(Character.toString(letter)) != -1) {
        		if (!letterAdded) {
        			sortedNames.add(Character.toString(letter));
        			letterAdded = true;
        			i--;
        		}
        		else 
        			sortedNames.add(searchString);
        	}
        	else {
        		while (resultingString.indexOf(Character.toString(letter)) == -1) {
        			letter++;
        		}
        		sortedNames.add(Character.toString(letter));
        		letterAdded = true;
        		i--;
        	}       		
        }

Open in new window

0
 
slightwv (䄆 Netminder) Commented:
If I understand what you want, where is the 'C' header?

Off the top of my head:  Loop through the array looking at first character of the last 'word'.  Add that to a new 'what I have array' then merge/sort them.
0
 
InquisitiveProgrammerAuthor Commented:
There would be a C in front of John Carter. I was just trying to provide a partial list example.

I am working on some code now. Hopefully you can offer some assistance when I have completed it.
0
Cloud Class® Course: CompTIA Cloud+

The CompTIA Cloud+ Basic training course will teach you about cloud concepts and models, data storage, networking, and network infrastructure.

 
slightwv (䄆 Netminder) Commented:
Can you run a Regular Expression against the array?

Honestly it would probably be better to do this at the source.  Can you access/change the source that passes you the array?
0
 
InquisitiveProgrammerConnect With a Mentor Author Commented:
I am receiving the information from a database and putting it into a string.

0
 
InquisitiveProgrammerConnect With a Mentor Author Commented:
This is what I'm working on:

 
char letter = 'A';
        boolean letterChanged = false;
        
        for (int i = 0; i < searchNames.size(); i++) {
        	String searchString = searchNames.get(i).toString();
        	int searchPattern = searchString.lastIndexOf(" ");
        	
        	String resultingString = searchString.substring(searchPattern + 1);
        	if (resultingString.indexOf(Character.toString(letter)) != -1 && letterChanged == false)
        		sortedNames.add(Character.toString(letter));
        	else if (resultingString.indexOf(Character.toString(letter)) != -1 && letterChanged == true)
        		sortedNames.add(searchString);
        	else if (resultingString.indexOf(Character.toString(letter)) == -1) {
        		//letterChanged = true;
        		i--;
        		letter++;
        		letterChanged = false;
        	}       		
        }

Open in new window


Right now my list looks like:

A
A
B
C
E
L
M

0
 
InquisitiveProgrammerAuthor Commented:
I should add my list of data also:

        searchNames.add("Elijah Arnman");
        searchNames.add("Tom Arnold");
        searchNames.add("Dan Bateman");
        searchNames.add("Tommy Canders");
        searchNames.add("Stefan Edberg");
        searchNames.add("Ivan Lendl");
        searchNames.add("Tomas Muster");

So you can see the right letters are being populated... just not the associated names. Also not sure why I'm getting an extra "A" on my list.
0
 
slightwv (䄆 Netminder) Commented:
Do you control the access/query from the database?

If so, what database?

>>Also not sure why I'm getting an extra "A" on my list.

The array you are populating isn't 'unique'.

There are a many links out there on unique array values:
http://code.hammerpig.com/fast-way-to-get-all-unique-values-from-a-list-in-java.html
possibly:
http://stackoverflow.com/questions/2235471/save-a-list-of-unique-strings-in-the-arraylist

If you can control the DB query, it might be better to get the list from there but I am a DB person and always think that.
0
 
slightwv (䄆 Netminder) Commented:
Cool.  I wasn't at a place where I could come up with the unique array logic.

Feel free to accept your post as the answer.
0
 
InquisitiveProgrammerAuthor Commented:
I was able to figure out the answer on my own.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.