This week's book giveaway is in the OCAJP 8 forum. We're giving away four copies of OCA Java SE 8 Programmer I Study Guide and have Edward Finegan & Robert Liguori on-line! See this thread for details.
hi this algo seems sorting in descending order. if u want to have ascending order sorting then change the comparision if() u 've like... if ( a[i].compareTo(a[i-1]) < 0 ).. it shd work... regards maulin
Jeremiah. First, due to the uninitialized stringArray, NullPointer exception was thrown when sort() method was called (check with MS-DOS for this exception). I put one line into init() to initialize stringArray... current inital value is "". for(int i=0;i<stringArray.length;i++) stringArray[i]=""; Second, as I dont really see why the sort() does not work as expected, I made some changes on the index inside the for loop of the sort() in a way I know how sorting is done,
then re-run the applet, the result is not perfect, but sorting is on the way. HTH [ April 24, 2003: Message edited by: chi Lin ]
not so smart guy still curious to learn new stuff every now and then
Joined: Mar 29, 2003
Chi, I tried it your way, but it seems like the sorting jumps about 8 index's down the array. not sure why your statement did get me somewhere. Thanks for that. [ April 24, 2003: Message edited by: Jeremiah Coleman ]
Just as an aside, since I'm not sure what your assignment entails, I would suggest using a List instead of an Array to hold your Strings and then sorting your list. Your sorting algorithm works fine, but as Balaji pointed out, the sortwing was failing when it encountered a null value in the Array. It woudl therefore do some sorting, but stop when it got the NullPointerException. If you filled the array with 10 variables it worked fine (albiet reversed; see Maulin's post). in general, you would still have to check for null, but in this instance you cnnot put null values into the list (just empty strings). And as an added benefit, you List is not limited to 10 entries. You can add as many as you like (and then see the performance difference between bubble-sorting 10 items and 300 items )
Piscis Babelis est parvus, flavus, et hiridicus, et est probabiliter insolitissima raritas in toto mundo.
Joined: Mar 29, 2003
Thanks you to everybody who replied. I appreciate your time. I tried the if (array[i]!=null) and it worked