简体   繁体   中英

How to organise alphabetically an array of objects in Java

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM