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 ?
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:
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.
Bookmarks