By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
448,837 Members | 1,629 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 448,837 IT Pros & Developers. It's quick & easy.

Comparable interface selection sort ........

P: 22
Hi,
I've created a Person Class with a comparable interface. And i've created an ArrayList People with varaibles from the person class in - First_name, Surname, Month(Birthday), Day(Birthday).
Now i need to use selection sort to sort my arraylist into birthday order. In the Person Class i have a method which gives each person a different number for every possible birthday there is.
I have created a selction sort but dont know how to call the birthday from my array list and then to sort it.

This is my code for the selection sort so far:

import java.util.ArrayList.*;


public class Main
{

public static void selectionSort(Comparable[] people)
{


for (int k = people.length-1; k>0; k --)
{
Comparable tmp;
int Large = 0;

for (int j=1; j<= k; j++)
{
if (people[j].compareTo(people[k]) <0)
{
Large = j;
}
tmp = people[Large];
people[Large] = people[k];
people[k] = tmp;
}
}
}
}

this method compiles but i need to sort the birthday and dont know how. Also i need to output the whole array in birthday order.

Any help would be very greatful.

Thanks
Dave
Apr 30 '07 #1
Share this Question
Share on Google+
1 Reply


10K+
P: 13,264
Hi,
I've created a Person Class with a comparable interface. And i've created an ArrayList People with varaibles from the person class in - First_name, Surname, Month(Birthday), Day(Birthday).
Now i need to use selection sort to sort my arraylist into birthday order. In the Person Class i have a method which gives each person a different number for every possible birthday there is.
I have created a selction sort but dont know how to call the birthday from my array list and then to sort it.

This is my code for the selection sort so far:

import java.util.ArrayList.*;


public class Main
{

public static void selectionSort(Comparable[] people)
{


for (int k = people.length-1; k>0; k --)
{
Comparable tmp;
int Large = 0;

for (int j=1; j<= k; j++)
{
if (people[j].compareTo(people[k]) <0)
{
Large = j;
}
tmp = people[Large];
people[Large] = people[k];
people[k] = tmp;
}
}
}
}

this method compiles but i need to sort the birthday and dont know how. Also i need to output the whole array in birthday order.

Any help would be very greatful.

Thanks
Dave
1.) Please use code tags if you have to post code.
2.) Did you read this tip?
Apr 30 '07 #2

Post your reply

Sign in to post your reply or Sign up for a free account.