aspose file tools*
The moose likes Beginning Java and the fly likes Why The Output Is Same... Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Why The Output Is Same..." Watch "Why The Output Is Same..." New topic
Author

Why The Output Is Same...

Krishna Potluri
Greenhorn

Joined: Apr 20, 2007
Posts: 19
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.
Keith Lynn
Ranch Hand

Joined: Feb 07, 2005
Posts: 2367
Is this what you want the while loop to do?

Chandra Bhatt
Ranch Hand

Joined: Feb 28, 2007
Posts: 1707


Comparable returns:
negative if objOne < objTwo
zero if objOne == objTwo
positive if objOne > objTwo

You was only returning -1, 1 and 0; Its not enough information for the
sorting method to arrange the item of the list in sorted manner.


Better you change the class Signature too as to avoid unnecessary casting in
the compareTo() method




Side Note: If your Comparable interface implementation is not parameterized with the <Boy>, as you did in your original code,
Collection.sort(list); gives compiler warning for unchecked operation.



Regards,
cmbhatt
[ April 25, 2007: Message edited by: Chandra Bhatt ]

cmbhatt
Krishna Potluri
Greenhorn

Joined: Apr 20, 2007
Posts: 19
Hi Chandra,
First, I am thankful to you for spending your precious time to give the reply.

When execute your code by adding following Strings to List, The OUPUT is not comming in sorted form.

If i changed the my source code by adding String elements to List as Follows:
________________________________________

list.add("SriRamD");
list.add("SriRamC");
list.add("SriRamB");
list.add("SriRamA");
__________________________________________________________________________

I am getting same output before and after sorting the List .

Kindly give me a solution that would really sort the elements in List.
It would be very great if i get needful info.

___________________________________________________________________________

Thanks and Regards,
Sri Krishna.
swarna dasa
Ranch Hand

Joined: Mar 15, 2007
Posts: 108
instead of using just the first character , compare the whole string

Chandra Bhatt
Ranch Hand

Joined: Feb 28, 2007
Posts: 1707
Hi Krishna,

You please once look at your compareTo() definition, which is based on first character of the name. What sorting order do you expect from that.



I think you want the following universal definition of the compareTo method:





Regards,
cmbhatt
Barry Gaunt
Ranch Hand

Joined: Aug 03, 2002
Posts: 7729
Please, general programming issues belong in one of our Java In General Forums. Moving...


Ask a Meaningful Question and HowToAskQuestionsOnJavaRanch
Getting someone to think and try something out is much more useful than just telling them the answer.
Krishna Potluri
Greenhorn

Joined: Apr 20, 2007
Posts: 19
Hi Chandra,

Thanks for your good reply. My code is working fine as expected. Once agian
thanks for your precious time for giving the reply.

Thanks and Regards,
Sri Krishna.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Why The Output Is Same...