java Collections.binarySearch() returning -2

binary-tree, collections, java, sorting

Solution

To perform a binary search, the collection must already be sorted. You are expecting `binarySearch` to sort it for you, but it doesn't sort it. It takes advantage of the assumption that it's already sorted. The Javadocs for `Collections.binarySearch` state:

Searches the specified list for the specified object using the binary search algorithm. The list must be sorted into ascending order according to the specified comparator (as by the sort(List, Comparator) method), prior to making this call. If it is not sorted, the results are undefined.

Add a call to `Collections.sort` before attempting a binary search.

StudentComparator sc = new StudentComparator();
Collections.sort(studentList, sc);

Then you can call `Collections.binarySearch`.

Problem

I have a text file which contains student information. I sort this information using the comparator class which implements the `comparator` interface.This sorts fine fine when calling `Collection.Sort();` I then use the `comparator` in the `binarySearch()` method. I have an int index that returns the index of the binary search. after debugging it seems that the `binarySearch()` isnt sorting the data properly because I get an index of 0 for 501 which is correct, an index of 1 for 503 which is incorrect and an index of -2 for 502. Is the `binarySearch` not using the comparator apppropriately and thus not sorting the data appropriately as its returning the data in its original form. ``` ================================================= First Name: Mark Last Name: Evans Registration: 501 Subject: Maths Assignment: 1 Homewok Mark: 70 Exam Mark: 80 Assignment: 2 Homewok Mark: 70 Exam Mark: 40 Subject: English Assignment: 1 Homewok Mark: 40 Exam Mark: 50 Assignment: 2 Homewok Mark: 60 Exam Mark: 70 Subject: Science Assignment: 1 Homewok Mark: 50 Exam Mark: 60 Assignment: 2 Homewok Mark: 80 Exam Mark: 45 Assignment: 3 Homewok Mark: 67 Exam Mark: 47 Assignment: 4 Homewok Mark: 38 Exam Mark: 57 ================================================= First Name: Linda Last Name: Evans Registration: 503 Subject: Maths Assignment: 1 Homewok Mark: 50 Exam Mark: 60 Assignment: 2 Homewok Mark: 70 Exam Mark: 50 ================================================= First Name: Joseph Last Name: Evanw Registration: 502 Subject: English Assignment: 1 Homewok Mark: 40 Exam Mark: 50 ``` I have a comparator class that works fine and sorts the data appropriately. ``` public class StudentComparator implements Comparator<Student> { @Override public int compare(Student s1, Student s2) { int registrationNumber1 = s1.getRegistrationNumber(); int registrationNumber12 = s2.getRegistrationNumber(); return registrationNumber1 - registrationNumber12; } } public void searchStudentByName() { int number = 503; int subjectNumber = 1; int index = Collections.binarySearch(studentList, new Student(number), new StudentComparator()); for (int i = 0; studentList.size() > i; i++) { if (i == index) { System.out.print(studentList.get(i)); } } } ```

Original source