• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Why The Output Is Same...

 
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 2412
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Is this what you want the while loop to do?

 
Ranch Hand
Posts: 1710
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 108
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
instead of using just the first character , compare the whole string

 
Chandra Bhatt
Ranch Hand
Posts: 1710
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Please, general programming issues belong in one of our Java In General Forums. Moving...
 
Krishna Potluri
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Or we might never have existed at all. Freaky. So we should cherish everything. Even this tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic