File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Why The Output Is Same...

 
Krishna Potluri
Greenhorn
Posts: 19
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2399
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Is this what you want the while loop to do?

 
Chandra Bhatt
Ranch Hand
Posts: 1710
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


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 ]
 
Krishna Potluri
Greenhorn
Posts: 19
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 108
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
instead of using just the first character , compare the whole string

 
Chandra Bhatt
Ranch Hand
Posts: 1710
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 7729
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please, general programming issues belong in one of our Java In General Forums. Moving...
 
Krishna Potluri
Greenhorn
Posts: 19
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic