File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes ArrayList selection Sort Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "ArrayList selection Sort" Watch "ArrayList selection Sort" New topic
Author

ArrayList selection Sort

Bryan Peach
Ranch Hand

Joined: Sep 08, 2009
Posts: 76
Hey there,
I created an ArrayList called names. The idea is that the user enters a bunch of names in any order and the program sorts them and prints a report sheet with the names sorted. Here is what I have for my sort method so far.

I get an error telling me that I cannot use > on the line

I realize this is probably some simple error but i don't understand why i cannot do that or how to get around it... help please....
Tim Sparg
Ranch Hand

Joined: Dec 29, 2006
Posts: 40
Hi Bryan

I'm not too sure what you are trying to use the '>' operator for.
that operator works for numerics, not string values.

what you can do is




But the best solution is to use the Collections class



This will sort the list by its natural ordering which should be alphabetical


There is no insanity so devastating in man's life as utter sanity
Bryan Peach
Ranch Hand

Joined: Sep 08, 2009
Posts: 76
Ok so now I have my sort method and a swap method... the sort method is supposed to figure out the order of where the strings should be... and my swap method is supposed to swap the contents in the string list i have PLUS make the same changes in two other lists i have made to keep the data compatible... here is the code for the two methods.

I get a few errors from these when trying to compile. First of all i get a error from the method call within the sort method saying it cannot find the symbol variable someSalesList. but thats the call i used all throughout the program for my methods and it worked... do you have to use a different call when calling a method from within a method?
The other errors i get are cannot find symbol method set in my swap method... any ideas?
Also, this sorting and swapping thing is very new to me... and while i understand the concept and how it is supposed to work the code seems very confusing to me... does the code look alright? or are there some serious problems going on... Thanks
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38424
    
  23
Not quite alphabetical, but what is jocularly called asciibetical. More details here, as well as the Google search earlier.

Note many of these questions are class exercises where using Collections#sort is prohibited.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38424
    
  23
You ought to use a static swapList method which takes the List, and the two indices as its parameters.
  • Use int rather than Integer.
  • Use i as your index in the for loop, not l because l can be difficult to distinguish from 1.
  • In a selection sort, you will probably want to start the loop with max = k;
  • It looks like a very awkward way to arrange sorting, trying to sort three Lists in parallel. Very difficult to maintain. You want a List of SalesAndExpenses and a SalesAndExpenses object which incorporates the three data you are using.
  • Maybe a different name for the SalesAndExpenses class would be appropriate.
  • Haev you looked through the List interface for set methods? Note how many parameters set requires.
  • John de Michele
    Rancher

    Joined: Mar 09, 2009
    Posts: 600
    I get a few errors from these when trying to compile. First of all i get a error from the method call within the sort method saying it cannot find the symbol variable someSalesList. but thats the call i used all throughout the program for my methods and it worked... do you have to use a different call when calling a method from within a method?


    Did you define someSalesList in your sortList() method? Is it an instance variable of your class?

    John.
    Bryan Peach
    Ranch Hand

    Joined: Sep 08, 2009
    Posts: 76
    John, I defined someSalesList in the main method and thats what i used to call all my other methods and it worked. So i'm not sure if there is a different way to call it from within a method. Here is the entire code. That might help to see what is going on here.
    Campbell Ritchie
    Sheriff

    Joined: Oct 13, 2005
    Posts: 38424
        
      23
    Do a search for my posts about Scanner. You will not get "Peach Bryan" by using the next() method; you will get "Peach", followed by an Exception when it tries to turn Bryan in to an int. You probably want "nextLine".
    Your totalExpenses will give the wrong result; you don't want a field at all, but a local variable, otherwise you get more expenses every tiem that method is called.

    You still have the design problem that you don't have a class which incorporates all the data. I now think, having see what you are doing, that a Salesman (or Saleswoman) class is what you want rather than SalesAndExpenses. It will probably also prevent you from trying to add a String to a List<Integer>.
    John de Michele
    Rancher

    Joined: Mar 09, 2009
    Posts: 600
    Bryan:

    It works in your main() method because someSalesList is in scope in your main() method. It doesn't work in your sortList() method because someSalesList is not in scope there. You need to either a) define someSalesList in the sortList() method, or (better) b) pass someSalesList to sortList(), so that it can work on it.

    John.
     
    Don't get me started about those stupid light bulbs.
     
    subject: ArrayList selection Sort