Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to use Selection Sort in an Array of Strings

Status
Not open for further replies.

mwn1218

MIS
Jan 17, 2005
11
US
So, I have to use a selection sort to sort an array of Strings even though I know this is not the most efficent way and am getting messed up in my coding. The array is coming from a separate class called Bank Account and this is what I have so far: I know how to sort 2 integers, but am totally lost on Strings. Can anyone give me any insight/direction on where to go? Thanks in advance!!

public static void SelectionSort(BankAccount[] BankArray){
String currentName;
int currentLetterIndex;
int currentAccount;

for (int i=BankArray.length-1; i>1; i--){
//Find the maximum in the list
currentName.getName() = BankArray.EqualsIgnoreCase(currentName.getName());
currentLetterIndex = i;

for (int j=i-1; j>0; j--){
if (currentName.compareTo(BankArray[j].getName())){
currentName.getName() = BankArray[j].EqualsIgnoreCase(currentName.getName());
currentLetterIndex = j;
}//close if
}//close 2nd for

//Swap i with current letter index if necessary
if (currentLetterIndex != i){
BankArray[currentLetterIndex] = BankArray;
BankArray[j] = currentName;
}//close if
}//close 1st for
}//close SelectionSort method
 
You may not assign something to a method:
Code:
  currentName.getName() = BankArray[i].EqualsIgnoreCase(currentName.getName());

When asking for help, you should stick to the coding-standards, and use lowercase beginning for objects, local variables and methods:
Code:
bankArray[i].equalsIgnoreCase(currentName.getName());

btw: if you use an indentation equivalent to 8 spaces, you don't need the ugly //-end-of-something - comments.

seeking a job as java-programmer in Berlin:
 
The Java API documentation would help you greatly with a problem such as this. You can download the API at http://java.sun.com/j2se/1.5.0/download.jsp.

But for this problem, I'll save you time. The String class has two methods that can help: compareTo() and compareToIgnoreCase(). You can use one of these like so:

Code:
String str1 = "jfklds";
String str2 = "jfdlka";
int result = str1.compareTo(str2);
compareTo() and compareToIgnoreCase() return the following:
-1 if str1 < str2
1 if str1 > str2
0 if str1 = str2

I'm sure you can handle the rest from there. Good luck.

Nick Ruiz
Webmaster, DBA
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top