www.webdeveloper.com
Results 1 to 3 of 3

Thread: Sorting using Bubble Sort help

  1. #1
    Join Date
    Mar 2004
    Posts
    14

    Sorting using Bubble Sort help

    Hiya i have an array of objects called messageArray which is created by converting an ArrayList to an Array

    Code:
      //Converts the ArrayList MessageList to an Array of Objects
    	public Object[] convertToArray() 
    	{
    		return messageList.toArray();
    	}
    	
    	public void printMessageArray() 
    	{
    		//Creates an Array of Objects called messageArray
    		Object[] messageArray = convertToArray();
     
    		//Loops around the messageArray and prints out its objects
    		for (int i = 0; i < messageArray.length; i++) 
    		{
    			String info = messageArray[i].toString();
    			System.out.println (info);
    		}
    	}
    The array has objects in it each called message and a message is made up of
    Code:
     String sender, String subject, String content, int day, int month, int year, int hour, int minute
    Now i want to sort the new Array which i have created by the sender by using bubble sort but not sure how can any help ?

    At the moe the print out is
    Code:
    Jon, Tips for Travel, You should really....., Received: 24/9/2004 10:30am
    Ian, Subject 2, Content 2, Received: 25/9/2004 11:30am
    Bob, Subject 3, Subject 3, Received: 24/9/2004 1:30pm
    Harry, Subject 4, Subject 4, Received: 24/9/2004 2:30pm
    but after the sort i want
    Code:
    Bob, Subject 3, Subject 3, Received: 24/9/2004 1:30pm
    Harry, Subject 4, Subject 4, Received: 24/9/2004 2:30pm
    Ian, Subject 2, Content 2, Received: 25/9/2004 11:30am
    Jon, Tips for Travel, You should really....., Received: 24/9/2004 10:30am

  2. #2
    Join Date
    Jul 2004
    Location
    Canada, eh
    Posts
    784
    Sorts on any string or numeric field of any Object. It does, of course, assume all Objects in the array are the same type:
    Code:
    public boolean bubbleSort(Object arr[], String fieldName, boolean numeric) {
      try {
        Class oClass=arr[0].getClass();
        Field f=oClass.getDeclaredField(fieldName);
        for(int i=0; i<arr.length; i++) {
          for(int j=0; j<arr.length-i-1; j++) {
            if(numeric) {
              if(f.getLong(arr[j+1])<f.getLong(arr[j])) {
                Object tmp=arr[j];
                arr[j]=arr[j+1];
                arr[j+1]=tmp;
              }
            } else {
              if(f.get(arr[j+1]).toString().compareToIgnoreCase(f.get(arr[j]).toString())<0) {
                Object tmp=arr[j];
                arr[j]=arr[j+1];
                arr[j+1]=tmp;
              }
            }
          }
        }
      } catch(Exception e) {
        return false;
      }
      return true;
    }
    You pass it the array of Objects, the name of the field to sort on (in your case, "sender"), and whether or not the field is numeric (if the field is a numeric type, pass true, otherwise false). If the field is a numeric type and you pass false, it will be treated as a string (i.e. 860 is greater than 1340 since it starts with an 8). If the sorting succeeded, the function returns true. If something went wrong, like a string with non-numeric characters is treated as a number, or you pass the name of a non-existant field, it returns false. Example usage:
    Code:
    boolean success=bubbleSort(someArray, "someFieldName", false);
    Last edited by HaganeNoKokoro; 11-20-2004 at 10:38 PM.
    Kids, kids... you tried your best, and you failed miserably; the lesson is: never try.

  3. #3
    Join Date
    Nov 2003
    Location
    Jerryville, Tejas
    Posts
    11,715
    Of course this is some kind of classwork so it's using simple concepts. In the real world* you would simplify the bubbleSort() method by passing it the array and a class that knows how to compare the objects in the array. I.e. you'd do it like Sun did the ordered collections.

    *Bubble sort would never be used.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles