简体   繁体   中英

Sorting an array of custom objects by values in Java

I am working on a CS-101 assignment and am only allowed to use a single array. I have an array that looks like the following:

[Song, Song, Album, Fiction, Movie, Nonfiction, Song]

Here is the hierarchy for background (requirements from my assignment):

"At the top level you will have a class called Library. Library will have three subclasses: Music, Book, and Movie. Music will have two subclasses: Song and Album. Book will have two subclasses: Fiction and Nonfiction. Movie, Fiction, Nonfiction, Song, and Album will not have any subclasses."

I am currently trying to write a method that will sort the Books by their ISBN number. So Fiction and Nonfiction are subclasses of my Book class, which is a subclass of Library.

I hold everything in Library myLibrary[] = new Library[100];

I'm not sure how to go about retrieving the ISBN's from the Books only and sort them since I am only allowed one array; otherwise I would love to make an array of Books, then sort those separately.

What are some hints / algorithms that I can utilize to accomplish this?

Update

I can post more code if needed. But this question is currently more focused on the approach.

The key here is setting up your inheritance correctly and than implementing the Comparable interface. See here for example: Java Comaprable and than calling .sort on your array of your parent type (in your case this would be myLibrary.sort();) Here is an example of how sort works on primitive types: Primitive type array sort

So

  1. Implement Comaparable on your subtypes
  2. Create your array of the parent type and populate it
  3. call sort on your array.

Good Luck!

Check if this works or not. (currently on tab so could not run code)

[I think after sorting your books will be saturated towards one side of the array. Please let me know the result]

/* book sorting is in decreasing order of ISBN, followed by non book items
The books will be at the beginning of array, other items towards the end */
Arrays.sort(myLibrary, new Comparator<Library>()
    {
        int compare(Library l1, Library l2){
            //if both are books then compare ISBN and return appropriate
            if((l1 instanceof Book) && (l2 instanceof Book)){
                Book b1=(Book)l1; Book b2=(Book)l2;
                if(b1.getISBN()<b2.getISBN) {
                    return -1;
                } else if(b1.getISBN()>b2.getISBN()) {
                    return 1;
                } else {
                    return 0;
                }
            }
            else {//if either one, or none are Book

                //if only l1 is Book, l2 is not
                if(l1 instanceof Book){
                    return 1;
                }

                //if only l2 is Book, l1 is not
                if(l2 instanceof Book){
                    return -1;
                }

                //none are Book
                return 0;
            }
        }
    }
);

Here you go...

As mentioned in the my previous answer Write a new Comparator and use the same for comparing Library objects.

Note: I haven't checked for null but you should do so...

class LibraryComparator implements Comparator<Library> {
    public int compare(Library l1, Library l2){
         // If Both are Book instance do the comparison
         if(l1 instanceof Book && l2 instanceof Book){
              // Assuming ISBN is a String or Long field in your class Book
              return ((Book)l1).getISBN().compareTo(((Book)l2).getISBN());
         } else {
         // Otherwise no change in ordering
              return 0;
              // You could specify sorting logic for Movie and Music here as well
         }
    }
}

And then you can sort the array like:

Arrays.sort(myLibrary, new LibraryComparator());

Without trying to give the actual implementation of the algorithm, you should do an in-place sort where priority can be done by:

1. Books have more priority than Music and Movies

2. If two objects are Books then priority is based on ISBN

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