I have an arraylist that is unsorted order and another arraylist that is the sorted order. I need to add a remove button to remove words from both the Original Order and Sorted Order but to remove with binarySearch I need to sort the original order. But I need to keep it unsorted...
int songIndex = Collections.binarySearch(song, titleArtistInput.getText());
int sortedSongIndex = Collections.binarySearch(sortedSong, titleArtistInput.getText());
//To test the values.
System.out.println(songIndex + " " + sortedSongIndex);
if (sortedSongIndex < 0)
{
titleArtistOutput.setText("That CD does not exist in the collection, please try again");
}
else if (sortedSongIndex >= 0)
{
sortedSong.remove(sortedSongIndex);
Collections.sort(song);
song.remove(Collections.binarySearch(song, titleArtistInput.getText()));
}
Is there a method that reverts the Collections.sort? or any way to do this without sorting the song ArrayList?
EDIT: I got it to work myself! Finally.
int sortedSongIndex = Collections.binarySearch(sortedSong, titleArtistInput.getText());
//if the Collections.binarySearch is negative (song not found), it will output
//"That CD does not exist in the collection, please try again", if the sortedSongIndex is positive
//(the song had been found!) and will remove the indexOf titleArtistInput.getText() from the ArrayLists
if (sortedSongIndex < 0)
{
titleArtistOutput.setText("That CD does not exist in the collection, please try again");
}
else if (sortedSongIndex >= 0)
{
sortedSong.remove(sortedSong.indexOf(titleArtistInput.getText()));
song.remove(song.indexOf(titleArtistInput.getText()));
}