Sorting an array of custom objects by values ​​in Java

I am working on the purpose of CS-101 and I am allowed to use only one array. I have an array that looks like this:

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

Here is the hierarchy for the background (requirements of my purpose):

"At the top level, you will have the Library class. The library will have three subclasses: Music, Book and Movie. Music will have two subclasses: Song and Album., Fiction, Non-Recognition, Song, and Album will not have any subclasses."

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

I keep everything in the Library myLibrary[] = new Library[100];

I'm not sure how to search for ISBNs only from books and sort them, since I am only allowed one array; otherwise, I would like to create an array of books and then sort them separately.

What are some tips / algorithms I can use to accomplish this?

Update

I can send more code if necessary. But this question is currently more focused on this approach.

+4
source share
4 answers

Without trying to give a real implementation of the algorithm, you should pretend in a place where priority can be made:

1. Books take precedence over music and films

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

0
source

The key here is the proper inheritance of your inheritance and the implementation of the Comparable interface. See here, for example: Java Comaprable , and not calling .sort in your array of your parent type (in your case it will be myLibrary.sort ();) Here is an example of how sorting works on primitive types: Sorting an array of primitive type

So

  • Implementing Comaparable on Your Slippers
  • Create your parent type array and fill it
  • sort the call in your array.

Good luck

+3
source

Check if it works or not. (currently tab, so the code failed to run)

[I think that 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; } } } ); 
+1
source

Here you go ...

As mentioned in the previous answer . Write a new Comparator and use the same to compare Library objects.

Note. I did not check for null, but you have to do it ...

 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 as follows:

 Arrays.sort(myLibrary, new LibraryComparator()); 
+1
source

Source: https://habr.com/ru/post/1435406/


All Articles