hey Guys
im using this bubble sort to sort an arraylist out but cant seem to output the sorted data out properly! Is there a return method i can use?

Damn ive been stuck hours on this one...

Anyone that could guide me to a solution would be great.


Note: the arrayList entering the bubble parametre in called studTranArray.
Also string sortby is set to "id"

private void bubbleSort(ArrayList<studentClass> sortedList, String sortBy)
      	{
            	//start with the first element in the array
            	for (int outer = 0; outer < sortedList.size() - 1; outer++)
            	{
                		//compare "outer" element with rest of elements one by one
               		 //swap if necessary
                		for (int inner = outer+1; inner < sortedList.size(); inner++)
               	 	{
                    		//if sorting by id compare ids
                   		 if (sortBy.equalsIgnoreCase("id"))
                    		{
                       			 if (sortedList.get(outer).getStudentID() > sortedList.get(inner).getStudentID())
                        		{
                           			 

					swapEm(sortedList, outer, inner);
                            			//displayData(sortedList);
                        		
                                
               //----------------------here i assumed the end of the sorting was and i could return sorted data to JList                  
                     int i = 0;
                     sortedList = studTranArray;
                     while (i<studTranArray.size())
              
                               {
                       tranList.addElement(studTranArray.get(i).studentName +"     "+ studTranArray.get(i).studentID);
                               i++;
                               }
                                 }	
                                 
            
                         }              
                	}
              }
    }


      private void swapEm(ArrayList<studentClass> sortedList, int outer, int inner)
      {

            studentClass temp = sortedList.get(inner);
            sortedList.set(inner, sortedList.get(outer));
            sortedList.set(outer, temp);
      }

Thanks to any help

When you mix tabs and spaces, the indentation often ends up looking terrible when you post. A tab is 8 spaces here. I always covert to spaces before posting.

//----------------------here i assumed the end of the sorting was and i could return sorted data to JList

This is in the middle of your code after the first swap, in the middle of a loop. Why would you think things would be sorted at this point? Looks like you have a bunch of other stuff going on in this function beyond just sorting. In my opinion, a sort function should do just that. Sort things. No adding or subtracting of elements to an ArrayList, no referencing of other class variables. You have an ArrayList to sort. Sort it. Do everything else somewhere else. It should probably therefore be a static function unless there is a good reason for it NOT to be a static function, and good OOP requires that it be irrelevant where the data comes from, so any references to "returning to a JList" should be removed since the function should not know or care about anything other than sorting what it's given as parameters. So again, make it a static function that relies on nothing other than the parameters it is passed.

Ok thanks VernonDozier for the advice!
what i cant understand is i have called the method bubblesort by:

bubbleSort(studTranArray, sortArray);

but how can obtain the sorted list? Sorry if its a dumb question, but im very new to programming...bare with me.

Ok thanks VernonDozier for the advice!
what i cant understand is i have called the method bubblesort by:

bubbleSort(studTranArray, sortArray);

but how can obtain the sorted list? Sorry if its a dumb question, but im very new to programming...bare with me.

It's not a dumb question. Often when you have a class that needs to be sorted, you write a compareTo function so you know when an object is "less than", "equal to", or "greater than" and you have the class implement the Comparable interface.

http://lkamal.blogspot.com/2008/07/java-sorting-comparator-vs-comparable.html
http://java.sun.com/javase/6/docs/api/java/util/Comparator.html
http://java.sun.com/javase/6/docs/api/java/lang/Comparable.html

Then you can use the sort function from Collections:

http://java.sun.com/javase/6/docs/api/java/util/Collections.html#sort(java.util.List)

Thus you don't have to write your own sort. It's decent practice though, so if you want to write a Bubble Sort, go for it. You may not choose to write a separate compareTo function, but generally that's how it's done. The point is you need some way of comparing two objects.

That said, to answer your question, I assume you are expected to do something like this (the function below is public and static. You can leave off the "static" part if you like and make it private like you had it before too).

public static void BubbleSort (ArrayList<studentClass> originalList, ArrayList<studentClass> sortedList)
{
    sortedList = (ArrayList) originalList.clone (); // note: this is a SHALLOW copy
    // now do a bubble sort on sortedList.  originalList remains the same.
}

Hey, i think VernonDozier has answered your problems just right, but can i ask why you're using bubblesort? its a good method, but i'd recommend you to try out quicksort or mergesort, they're highly efficient.

Hey guys, it seems i didnt need an arraylist at all as i could add Objects direct to defaultListModel. Then if should be much easier to sort.
The reason why im using bubblesort is thats what is required for the assignment :( I will update you if everything goes to plan.

Guys thanks for your time and help.

Hey guys, it seems i didnt need an arraylist at all as i could add Objects direct to defaultListModel. Then if should be much easier to sort.
The reason why im using bubblesort is thats what is required for the assignment :( I will update you if everything goes to plan.

Guys thanks for your time and help.

Just saw and commented on your other thread. I'm not sure that sorting a JList is any easier than sorting an ArrayList. You still somehow have to provide a way to compare two objects. After doing that, Bubble Sort is pretty much the easiest sort to code.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.