1.

Solve : stuck in Names Sorting Problem in Java :(?

Answer»

I am composing a code which requires a list of NAMES sorted by the Last Names. The Last name should be in upper CASE and put in front of the first name. It looks like the example:

Example LISTINGS:
List of Names for all customers who have paid bills:
***************************************************************
BROWEN Alen
BROWN John
GREEN Abbey
SHAW Michelle
SHOOK Michael
WHITE Alice
***************************************************************

However, my code doesn't work like this. Can ANYBODY tell me how I can make it work? Much appreciated. My code is as follow.
"customers" has been defined as a HashSet collection.
"Person" is a previously defined class, and all its methods used here have been defined.

Code: [Select]public void listCustomersNames_Paid()
{
System.out.println("List of Names for all customers who have paid bills:");
System.out.println("***************************************************************");
for(Person oCustomer : customers){
String[] nameArray = oCustomer.getName().split(" ");
String str10 = nameArray[0];
String str11 = nameArray[1].toUpperCase();

Iterator<Person&GT; it = customers.iterator();
while(it.hasNext()) {
nameArray = oCustomer.getName().split(" ");
String str20 = nameArray[0];
String str21 = nameArray[1].toUpperCase();
if(str11.compareTo(str21) > 0) {
String tempstr1 = str11;
str11 = str21;
str21 = tempstr;

String tempstr2 = str10;
str10 = str20;
str20 = tempstr2;
}
it.next();
}
System.out.println(str11 + " " + str10 + " of " + oCustomer.getAddress());
}
System.out.println("***************************************************************");
}



Discussion

No Comment Found