Hi,
I wrote a program whose code is as follows:
package com.kris.tigerv.ch2;
import java.util.List;
import java.util.LinkedList;
import java.util.Collections;
import java.util.Iterator;
import org.apache.log4j.Category;
class Boy implements Comparable{
private
String name = null;
Boy(String name){
this.name = name;
}
public String toString(){
return "[ "+name+" ]";
}
public int compareTo(Object ob){
int i = 0;
int flag = 0;
int result = 0;
while(this.name.length()< i) {
if(this.name.charAt(i)> ((Boy)ob).name.charAt(i)){
flag = 1;
result = 1;
}
else if(this.name.charAt(i)< ((Boy)ob).name.charAt(i)){
flag = 1;
result = -1;
}
else{
i++;
}
}
if (flag == 0) {
result = 0;
}
return result;
}
}
public class TypeSafeListWithIterator_V1 {
public static void main(String[] args) {
List<Boy> list = new LinkedList<Boy>();
list.add(new Boy("Sri"));
list.add(new Boy("Krishna"));
list.add(new Boy("Prasad"));
list.add(new Boy("Potluri"));
//Log4j class
Category cat = Category.getInstanceTypeSafeListWithIterator_V1.class);
cat.info("...List Elements Before Sort...");
Iterator<Boy> iterator = list.iterator();
while(iterator.hasNext()) {
cat.info(iterator.next());
}
Collections.sort(list);
cat.info("...List Elements After Sort...");
Iterator<Boy> iterator1 = list.iterator();
while(iterator1.hasNext()){
cat.info(iterator1.next());
}
}
}
___________________________________________________________________________
*******WHY THE List ELEMENTS ARE NOT DISPLAYED IN SORTED ORDER Based on BOY name ***
Kindly help me to resolve the issue.
__________________________________________________________________________
Thanks In Advance,
Kris.