File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Java in General and the fly likes sorting string with out using predefined java methods Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "sorting string with out using predefined java methods " Watch "sorting string with out using predefined java methods " New topic
Author

sorting string with out using predefined java methods

saikrishna cinux
Ranch Hand

Joined: Apr 16, 2005
Posts: 689
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:


A = HARDWORK B = LUCK/FATE If C=(A+B) then C=SUCCESSFUL IN LIFE else C=FAILURE IN LIFE
SCJP 1.4
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38351
    
  23
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

Joined: Apr 25, 2005
Posts: 809
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


Asking Smart Questions FAQ - How To Put Your Code In Code Tags
Naseem Khan
Ranch Hand

Joined: Apr 25, 2005
Posts: 809
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
 
subject: sorting string with out using predefined java methods