DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

+ Reply to Thread
Results 1 to 3 of 3
  1. #1
    Join Date
    Nov 2004
    Posts
    7

    bubble sorting 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
    Nov 2004
    Location
    Minnesota
    Posts
    99
    Bubble Sort generic algorithm, which was 1 google away. Adapt to your language of choice:
    Code:
    0037    procedure BubbleSort( ListToSort : in out Lists) is
    0038 
    0039       ListIsSorted : BOOLEAN := FALSE;
    0040 
    0041    begin -- BubbleSort
    0042       while not( ListIsSorted) loop
    0043          ListIsSorted := TRUE;
    0044          for ThisIndex in ListToSort'FIRST ..
    0045                           Indexes'PRED( ListToSort'LAST) loop
    0046              if not SortRelation( ListToSort( ThisIndex),
    0047                     ListToSort( Indexes'SUCC( ThisIndex))) then
    0048                 Swap( ListToSort( ThisIndex),
    0049                       ListToSort( Indexes'SUCC( ThisIndex)) );
    0050                 ListIsSorted := FALSE; 
    0051              end if;
    0052          end loop;
    0053       end loop;
    0054    end BubbleSort;

  3. #3
    Join Date
    Nov 2004
    Posts
    7

    Talking

    thanx alot got it workin now cheers

Bookmarks

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
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


Top DevX Stories

Easy Web Services with SQL Server 2005 HTTP Endpoints
JavaOne 2005: Java Platform Roadmap Focuses on Ease of Development, Sun Focuses on the "Free" in F.O.S.S.
Wed Yourself to UML with the Power of Associations
Microsoft to Add AJAX Capabilities to ASP.NET
IBM's Cloudscape Versus MySQL


Sponsored Links