Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

sorting string with out using predefined java methods

 
saikrishna cinux
Ranch Hand
Posts: 689
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi, i am trying to sort some strings with out using predefined methods in java
but it is not working properly

can any one gind me the bugs in this program???
:roll: :roll:
 
Campbell Ritchie
Sheriff
Pie
Posts: 49442
62
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Your compare method is supposed to return a positive answer if a > b, zero if the two are the same, and a negative answer if a < b.
I can see no way to get a negative answer out of your compare method.

BTW. You will find it quicker to use arithmetic on the char values: you can calculate char1 - char2.

You are using l (don't use small L; it is too easy to confuse with the number 1) as the count of comparisons. But that will give a zero result if you compare the Strings "Camp" and "Campbell."

Initialise your loop counter in the for statement.
Initialise your temp value in the if block.
 
Naseem Khan
Ranch Hand
Posts: 809
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I tested your application and came to following conclusion....

if leftChar<rightChar then set r=-1;
if leftChar==rightChar then set r=0;
if leftChar>rightChar then set r=+1;

With these changes and slightly I changed inner for loop,

here is the code...




Naseem
 
Naseem Khan
Ranch Hand
Posts: 809
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
do one more thing, before passing it to myStrCmp(String a,String b), call trim() otherwise you will get wrong result.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic