This week's book giveaway is in the Clojure forum.
We're giving away four copies of Clojure in Action and have Amit Rathore and Francis Avila on-line!
See this thread for details.
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

sorting string with out using predefined java methods

 
saikrishna cinux
Ranch Hand
Posts: 689
  • 0
  • 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: 47288
52
  • 0
  • 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
  • 0
  • 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
  • 0
  • 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.
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic