1

i'm trying to find a good way to sort the search results according to relevance after performing a search with a SearchView in Android. For me relevance means the number of matches in two SQLite text columns

I'm using a CursorLoader and there the sort order can be given to the constructor at the end

CursorLoader tLoader = new CursorLoader(
    getActivity(), ContentProviderM.ARTICLE_CONTENT_URI,
    tProj, tSel, tSelArgs, SORT_ORDER);

(or set using the setSortOrder (String sortOrder) method)

But i need more flexibility than this because i'm looking to sort on the number of matches rather than just on one or two columns

The only solution i can see myself is to add another column in my SQLite table, do some processing, and supply that column as the sort column to the CursorLoader

Now for my question: What is the best way to supply the sort order information to the CursorLoader using SQLite syntax, avoiding having to add a new column? (And what could this SQLite code look like?) Also, i'd like to ask more in general: Is there a different solution to this problem that i've missed?

Grateful for any help! And with kind regards, Tord

sunyata
  • 1,843
  • 5
  • 27
  • 41

2 Answers2

0

Depending on the content provider, if it just pass to the orderBy field, you can do anything.

SQLiteDatabase query

orderBy How to order the rows, formatted as an SQL ORDER BY clause (excluding the ORDER BY itself). Passing null will use the default sort order, which may be unordered.

you can do whatever you want, this is just the line after ORDER BY

P.S. It is totally depending on the Content Provider, it it choose to ignore the parameter, you can do nothing.

Derek Fung
  • 8,171
  • 1
  • 25
  • 28
  • Thank you for this, your answer has helped me gain some clarity into this and i've updated my question – sunyata Sep 13 '15 at 15:34
  • Can you provide a specific sqlite statement that i could use in a situation like this? I know it wasn't clear originally in the question but i've now updated it – sunyata Sep 13 '15 at 17:49
0

i found a "workaround" for this problem.

After investigating different ways to write sqlite code i ended up just adding a new table column just for sorting. This column simply stores an integer and is updated every time that the user performs a search, right before the CursorLoader is created

Advantages:

  • We can now do all of the relevance calculations in Java code

Drawbacks:

  • Relevance calculation is done as the search is done so if we have a large number of items it may take some time to process everything
sunyata
  • 1,843
  • 5
  • 27
  • 41