Hi my name is Knightofdurham but my friends call me Knight anyways i am a student in the Uk doing information systems and management. ANyways the reason I have registered to this website is because i need help with one of my java programs. Basically at the moment it sorts people in my array list by last name however i want to know how to sort by birthday to be more specific by their month. Its just that when i try to use the comparable on an int it just doenst work. To be honest my java is a bit weak but i am working on it. I am not asking you guys to do my homework cause i have done it but I am stumped and need some help please. here is my code
class Person
{
private String lastName;
private String firstName;
private int day;
private int month;
//----------------------------------------------------------
public Person(String last, String first, int d, int m)
{ // constructor
lastName = last;
firstName = first;
day = d;
month = m;
}
//----------------------------------------------------------
public void displayPerson()
{
System.out.print(" Last name: " + lastName);
System.out.print(", First name: " + firstName);
System.out.println(", Day: " + day);
System.out.println(", Month:" + month);
}
//----------------------------------------------------------
public String getLast() // get last name
{ return lastName; }
} // end class Person
////////////////////////////////////////////////////////////////
class ArrayInOb
{
private Person[] a; // ref to array a
private int nElems; // number of data items
//-------------------------------------------------------------
public ArrayInOb(int max) // constructor
{
a = new Person[max]; // create the array
nElems = 0; // no items yet
}
//-------------------------------------------------------------
// put person into array
public void insert(String last, String first, int day, int month)
{
a[nElems] = new Person(last, first, day, month);
nElems++; // increment size
}
//-------------------------------------------------------------
public void display() // displays array contents
{
for(int j=0; j<nElems; j++) // for each element,
a[j].displayPerson(); // display it
System.out.println("");
}
//-------------------------------------------------------------
public void insertionSort()
{
int in, out;
for(out=1; out<nElems; out++) // out is dividing line
{
Person temp = a[out]; // remove marked person
in = out; // start shifting at out
while(in>0 && // until smaller one found,
a[in-1].getLast().compareTo(temp.getLast())>0)
{
a[in] = a[in-1]; // shift item to the right
--in; // go left one position
}
a[in] = temp; // insert marked item
} // end for
} // end insertionSort()
//-------------------------------------------------------------
} // end class ArrayInOb
////////////////////////////////////////////////////////////////
need help i think with my insdertion sort.
thanks
guys i apologize for the lack of code wrapping have no idea how to do it.