I have a class, Library, that contains an array of Book objects, and I need to sort the array based off the properties of Book, either Title or PageNumber. The problem is im not allowed to use the Comparable class with Book. How would you recommend I sort the array of Books in library? Write my own sort? Or is there an easier way? If you need snippets of code, just ask!
Share
You can provide a
Comparatorfor comparing any type you wish,Comparableor otherwise.For Arrays and Collections you use
Even sorted collections like TreeSet can take a custom Comparator
e.g.