0

I'm looking to sort a list based on most closely matching to a query. So for my case, I have a custom object called movie, which has several fields, the one I'm concerned with is title. When I do the search, the OMDb API returns some results but unfortunately, the API's results are a little wonky sometimes in the way they're sorted.

For instance, searching "Guard" returns "The Old Guard" first, even before movies titled "Guard" and "The Guard". So what I'm looking to do is take the query string, in this case "guard", and sort the list of movie results based on how closely movie.title matches the query. I'm using an ArrayList to store these objects. Thanks for any help.

daniel-eh
  • 344
  • 3
  • 13
  • This is done by writing a Comparator that you can pass to `arrayList.sortedBy()`. But the algorithm that you put in the Comparator for determining what is closest to the search term will be up to you. That could be subjective, so there isn't a standard way to do it. – Tenfour04 Dec 30 '20 at 23:11
  • Mmmm. For one possible approach, you might want to search on terms such as 'string metric', 'string similarity index', and 'edit distance'. But that might not fit your needs very well, judging from the example in the question. – gidds Dec 30 '20 at 23:42

0 Answers0