1

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);       
    }    
}
Momissimo
  • 33
  • 7
  • If this question not a duplicate of the following Q&A then it at least shows how to stat: [Sort Java Collection](https://stackoverflow.com/questions/6957631/sort-java-collection) – LuCio Oct 21 '18 at 17:54
  • The thing is that I do not want to use lists – Momissimo Oct 21 '18 at 17:59
  • You have to use a list or something similar. `Collection` is an interface, you can't call `new` on it. – markspace Oct 21 '18 at 18:02
  • you want to sort it by making some function ? – Syed Hamza Hassan Oct 21 '18 at 18:02
  • First of all, you should follow the Java naming conventions. It's really poor idea to write your own class named `Collection`, since it is an interface in the JDK. Rename it to something like `AlbumCollection`. Also your name methods should start with lower-case, so `public void Add (Album a)` has to be renamed as `public void add (Album a)`. BTW, something related to your question can be easily coded. – zlakad Oct 21 '18 at 18:15
  • zlakad, thanks. I have done modification as you said, could you give me any example? – Momissimo Oct 21 '18 at 18:24
  • Possible duplicate of [Sort array alphabetically, upper letter always first](https://stackoverflow.com/questions/42074131/sort-array-alphabetically-upper-letter-always-first) – RyanInBinary Oct 21 '18 at 18:34
  • Possible duplicate of [Sort an array of objects alphabetically](https://stackoverflow.com/questions/19561067/sort-an-array-of-objects-alphabetically) – Stephen Tetreault Oct 21 '18 at 18:41

1 Answers1

1

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);  
}