0

So I have an ArrayList of object 'Book' (The Item objects contain data of Title, Author, ID) and the object class' getTitle() method returns the title of the book as a string. How would I mergeSort the existing ArrayList into a new ArrayList of 'Item' that's ordered Alphabetically by title?

I also need to create a method called searchTitle() that takes user input of String as a parameter and uses binary searching to find the book title in the newly sorted list and then if it finds it, it will return the entire Item with the matching title.

I know how binary searching works I'm just not sure how I'm going to do that with the strings that the Book.getTitle() method will return...

Chase Allen
  • 33
  • 1
  • 7
  • look into Comparators – MMelvin0581 May 02 '18 at 19:10
  • 1
    first part of question is duplicate of https://stackoverflow.com/questions/5178092/sorting-a-list-of-points-with-java/5178106 Second part you can look into `compare` method – jmj May 02 '18 at 19:11
  • What's the relationship between `Item` and `Book`? – shmosel May 02 '18 at 19:11
  • 1
    I think you're looking for `books.sort(Comparator.comparing(Book::getTitle));`. – shmosel May 02 '18 at 19:15
  • 1
    As for the binary search, just call [`Collection.binarySearch()`](https://docs.oracle.com/javase/8/docs/api/java/util/Collections.html#binarySearch-java.util.List-T-java.util.Comparator-) with the same comparator. – shmosel May 02 '18 at 19:24
  • You may also consider building a `Map booksByTitle` and using it instead of binary search. – lexicore May 02 '18 at 22:09

0 Answers0