?
Solved

Help sorting an array

Posted on 2011-10-28
10
Medium Priority
?
173 Views
Last Modified: 2012-05-12
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.

0
Comment
Question by:InquisitiveProgrammer
  • 6
  • 4
10 Comments
 
LVL 78

Expert Comment

by:slightwv (䄆 Netminder)
ID: 37048291
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
 
LVL 1

Author Comment

by:InquisitiveProgrammer
ID: 37048294
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
 
LVL 78

Expert Comment

by:slightwv (䄆 Netminder)
ID: 37048343
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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
LVL 1

Assisted Solution

by:InquisitiveProgrammer
InquisitiveProgrammer earned 0 total points
ID: 37048359
I am receiving the information from a database and putting it into a string.

0
 
LVL 1

Assisted Solution

by:InquisitiveProgrammer
InquisitiveProgrammer earned 0 total points
ID: 37048370
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
 
LVL 1

Author Comment

by:InquisitiveProgrammer
ID: 37048378
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
 
LVL 78

Expert Comment

by:slightwv (䄆 Netminder)
ID: 37048435
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
 
LVL 1

Accepted Solution

by:
InquisitiveProgrammer earned 0 total points
ID: 37048592
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
 
LVL 78

Expert Comment

by:slightwv (䄆 Netminder)
ID: 37048616
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
 
LVL 1

Author Closing Comment

by:InquisitiveProgrammer
ID: 37068440
I was able to figure out the answer on my own.
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
The viewer will learn how to implement Singleton Design Pattern in Java.
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
Suggested Courses
Course of the Month13 days, 20 hours left to enroll

809 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question