Reputation: 33
I am trying to simulate a library of Albums. But I would also be able to organise the contents of the library alphabetically by the author's name. Any help in how to organise the contents of the array of objects alphabetically?
I have created a Class called Album, which I use to create my objects
public class Album {
private String author;
private String name;
private String year;
public Album(String a, String n, String y) { // constructor
author = a;
name = n;
year = y;
}
public String toString()
{
return author +","+ name + "," + year;
}
}
The class Collection is used to store the objects into an array
public class AlbumCollection {
public Album collection[]= new Album[10];
private int numAlbums = 0;
public void add (Album a){
if (numAlbums >= collection.length){
Album newcollection[]= new Album [collection.length * 2];
for (int n = 0; n < numAlbums; n ++){
newcollection[n] = collection[n];
}
newcollection = collection;
}
collection[numAlbums] = a;
numAlbums = numAlbums + 1;
}
public String toString()
{
String details = "";
for ( int p = 0; p < collection.length ; p ++)
{
details = details + collection[p] + "\n" ;
}
details += "\n";
return details;
}
}
This is the class that I am using to create the Album Objects
public class TestCollection {
public static void main(String[] args) {
AlbumCollection c = new AlbumCollection();
c.add( new Album("DaftPunk","Discovery","2001"));
c.add( new Album ("Pink Floid","The Dark Side Of The Moon","1973"));
c.add( new Album( "The Clash", "London Calling", "1979"));
System.out.print(c);
}
}
Upvotes: 1
Views: 170
Reputation: 51
I had to change the compareTo method to sort by the author.
public class Album {
private String author;
private String name;
private String year;
public Album(String a, String n, String y) { // constructor
author = a;
name = n;
year = y;
}
public String toString()
{
return author +","+ name + "," + year;
}
public int compareTo(Album a) {
// usually toString should not be used,
// instead one of the attributes or more in a comparator chain
return author.compareTo(a.author);
}
} And i added method sort to sorting elements of array:
public class Collection {
public Album collection[]= new Album[10];
private int numAlbums = 0;
public void Add (Album a){
if (numAlbums >= collection.length){
Album newcollection[]= new Album [collection.length * 2];
for (int n = 0; n < numAlbums; n ++){
newcollection[n] = collection[n];
}
newcollection = collection;
}
collection[numAlbums] = a;
numAlbums = numAlbums + 1;
}
public String toString()
{
String details = "";
for ( int p = 0; p < numAlbums ; p ++)
{
details = details + collection[p] + "\n" ;
}
details += "\n";
return details;
}
public void sort(){
for(int i=0;i<numAlbums;i++){
for(int j=i;j<numAlbums-1;j++){
if(collection[j].compareTo(collection[j+1])>0){
Album tmp =collection[j];
collection[j]=collection[j+1];
collection[j+1]=tmp;
}
}
}
}
}
You can not use the length of an array, if you store the number of authors, because you will print null values
public static void main(String[] args) {
Collection c = new Collection();
c.Add( new Album("DaftPunk","Discovery","2001"));
c.Add( new Album ("Pink Floid","The Dark Side Of The Moon","1973"));
c.Add( new Album( "The Clash", "London Calling", "1979"));
c.sort();
System.out.print(c);
}
Upvotes: 1