简体   繁体   English

在 Android 上不区分大小写的领域 findAll

[英]Realm findAll with case Insensitive on Android

I have an Contact who have aa colum name, that have names on Lower and upercase.我有一个联系人,他有一个列名,名字是小写和大写。 I'd want to findAll with Case.INSENSITIVE and i didn't match any answers.我想 findAll 与 Case.INSENSITIVE 并且我没有匹配任何答案。 How i should do that?我该怎么做?

I'm doing it :我正在这样做:

realm.where(Contact.class).findAllAsync().sort("name")

As of 2020, Realm does have support for case insensitive queries.截至 2020 年,Realm 确实支持不区分大小写的查询。 I use it in one of my apps and works fine:我在我的一个应用程序中使用它并且工作正常:

String searchText = "john"; // Some name
RealmResults<Person> persons = realm.where(Person.class).contains("username", searchText, Case.INSENSITIVE);

It'll pull John, JOHN, john, etc.它会拉动约翰、约翰、约翰等。

Case.INSENSITIVE flag is not yet supported for sort method.排序方法尚不支持 Case.INSENSITIVE 标志。 You should get all results and then sort it manually :您应该获得所有结果,然后手动对其进行排序:

RealResults<Contact> list = realm.where(Contact.class)
    .findAll()
    .sort("name");

Collections.sort(list, new Comparator<Contact>() {
    @Override
    public int compare(Contact c1, Contact c2) {
        return c1.getName().compareToIgnoreCase(c2.getName());
    }
});

Realm doesn't have a native solution for that question. Realm 没有针对该问题的本地解决方案。

I have solved it creating an extra colum with lowerCases "name", and then a sort by lowerCase, like this:我已经解决了它创建一个带有小写“名称”的额外列,然后按小写排序,如下所示:

realm.where(Contact.class).findAllSortedAsync("lowerCase"));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM