This week's book giveaway is in the General Computing forum. We're giving away four copies of Arduino in Action and have Martin Evans, Joshua Noble, and Jordan Hochenbaum 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
Everything is Correct with ur code...the only thing is u haven't checked for null values while displaying and sorting...added those two checks...check whether it is working or not
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
Jere Johnson
Greenhorn
Joined: Mar 29, 2003
Posts: 28
posted
0
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.
Jere Johnson
Greenhorn
Joined: Mar 29, 2003
Posts: 28
posted
0
Thanks you to everybody who replied. I appreciate your time. I tried the if (array[i]!=null) and it worked
I agree. Here's the link: http://ej-technologies/jprofiler - if it wasn't for jprofiler, we would need to
run our stuff on 16 servers instead of 3.
subject: need help sorting a string array alphabetically