Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>If you can use <code>Comparators</code>, write one for each type of sorting you need, e.g., ascending for book title and descending for page number. The <code>compare</code> method of a <code>Comparator</code> must return positive if the first argument is larger than the second, negative if the first is smaller and zero if they are equal.</p> <pre><code>import java.util.Comparator; import java.util.List; import java.util.Arrays; class Book{ String title; int pageNumber; public Book(String title, int pageNumber){ this.title = title; this.pageNumber = pageNumber; } String getTitle(){ return title; } int getPageNumber(){ return pageNumber; } public String toString(){ return "(" + title + ", " + pageNumber + " pages)"; } } public class Library{ // These variables are static because you don't need multiple copies // for sorting, as they have no intrinsic state. static private Comparator&lt;Book&gt; ascTitle; static private Comparator&lt;Book&gt; descPageNumber; // We initialize static variables inside a static block. static { ascTitle = new Comparator&lt;Book&gt;(){ @Override public int compare(Book b1, Book b2){ return b1.getTitle().compareTo(b2.getTitle()); } }; descPageNumber = new Comparator&lt;Book&gt;(){ @Override public int compare(Book b1, Book b2){ // Java 7 has an Integer#compare function return Integer.compare(b1.getPageNumber(), b2.getPageNumber()); // For Java &lt; 7, use // Integer.valueOf(n1).compareTo(n2); // DO NOT subtract numbers to make a comparison such as n2 - n1. // This can cause a negative overflow if the difference is larger // than Integer.MAX_VALUE (e.g., n1 = 2^31 and n2 = -2^31) } }; } private Book[] books; public Book[] getBooks(){ return books; } public void sortAscTitle(){ Arrays.sort(books, ascTitle); } public void sortDescPageNumber(){ Arrays.sort(books, descPageNumber); } public Library(Book[] books){ this.books = books; } public static void main(String[] args){ Library library = new Library( new Book[]{ new Book("1984", 123), new Book("I, Robot", 152), new Book("Harry Potter and the Philosopher's Stone", 267), new Book("Harry Potter and the Goblet of Fire", 759), new Book("The Bible", 1623) }); library.sortAscTitle(); System.out.println(Arrays.toString(library.getBooks())); library.sortDescPageNumber(); System.out.println(Arrays.toString(library.getBooks())); } } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload