1

I want a list of items in which I want to perform a search. I want items to be filtered based upon the query in case sensitive order. For Example if user searches for milk the order should be like milk Milk butter milk Butter MILK Below is my current query but since I have added Case.Insesitive it gives me any random order based on the position of item in table.

  mRealm.where(Product.class).contains("productTags.name", tag,Case.INSENSITIVE).findAll();
swati
  • 1,263
  • 2
  • 17
  • 27
  • Realm currently doesn't support case insensitive searches. Check it for more details https://realm.io/docs/java/latest/#case-insensitive-queries or can check there limitations link https://realm.io/docs/java/0.79.0/#current-limitations –  Jul 06 '17 at 07:11

1 Answers1

5

You need to use findAllSorted, like:

    realm.where(Product.class)
            .contains("productTags.name", tag, Case.INSENSITIVE)
            .findAllSorted("productTags.name", Sort.ASCENDING);

But the sorting will be based on the ASCII value of the String, which by the looks of it isn't enough for you. In that case you need to do the sorting yourself by using a Comparator

Christian Melchior
  • 19,978
  • 5
  • 62
  • 53
  • @ Christian Melchior , i have one doubt i have applied the filter in listtview , its working fine , i am filtering the contact by search query , i can make the search by number but my condition is to make search contact by name and number , how to make the search by concept. This query i am using to search data realm.where(RealmPhoneCallLogs.class) .contains("number", text, Case.INSENSITIVE) .findAllSorted("id") – Prabha Karan Jul 06 '17 at 07:32
  • @PrabhaKaran The question was about `Product` and `productTags.name`, if you have a different question then you should edit the question. Btw, link sorts are supported since Realm 3.0.0. – EpicPandaForce Jul 06 '17 at 07:34
  • @Christian Melchoir , You mean after getting the result with the current query I should again sort this using custom Comparator.If I will do so it will be an overhead in terms of user experience because the products are very large in number – swati Jul 06 '17 at 08:54
  • Yes, but that is kinda the only solution if Realm doesn't have inbuilt support for the specific sorting you want. – Christian Melchior Jul 06 '17 at 11:18