2016-12-29 20 views
0

EditTextに入力された特定の文字列に対してオブジェクトのグループを既にフィルタリングしましたが、指定した文字列の位置でそのリストをソートする必要があります。オブジェクトの検索と並べ替えリスト

私はすでに

public void setFilter(String query) { 
    visibleList = new ArrayList<>(); 
    query = query.toLowerCase(Locale.getDefault()); 
    for (AccountProfile accountProfile : accountProfileList) { 
     if (accountProfile.getName().toLowerCase(Locale.getDefault()) 
       .contains(query)) 
      visibleList.add(accountProfile); 
    } 

    Collections.sort(visibleList, new AccountNameComparator()); 


} 

AccountNameComparator

public class AccountNameComparator implements Comparator<AccountProfile> { 
@Override 
public int compare(AccountProfile first, AccountProfile second) { 
    return first.getName().compareTo(second.getName()); 
} 

}

リストがソートされているが、それは私がソートする必要がgetname()に基づいており、この

フィルタ機能を行っています

の特定の部分文字列を持つリストsort that list with the position of the specified string

+3

以下のようにちょうどあなたが必要とするものは何でもと比較する方法を変更します。 –

答えて

1

、あなたはこのような何かを試みることができる:

public class AccountNameComparator implements Comparator<AccountProfile> { 
    private final String query; 
    public AccountNameComparator(String query) { 
    this.query = query; 
    } 
    @Override 
    public int compare(AccountProfile first, AccountProfile second) { 
     Integer f = first.getName().indexOf(this.query); 
     Integer s = second.getName().indexOf(this.query); 
     return f.compareTo(s); 
    } 
} 
+1

文字列firstName = first.getName()。toLowerCase(); 文字列secoundName = second.getName()。toLowerCase();小さな変化、それは私のために働いた。 –

0

上記の回答のわずかな変化があります:

public class AccountNameComparator implements Comparator<AccountProfile> { 
private final String query; 

public AccoluntNameSortComparator(String query) { 
    this.query = query; 
} 

@Override 
public int compare(AccountProfile first, AccountProfile second) { 
    String firstName = first.getName().toLowerCase(); 
    String secoundName = second.getName().toLowerCase(); 
    query = query.toLowerCase(); 
    Integer f = firstName.indexOf(query); 
    Integer s = secoundName.indexOf(query); 
    return f.compareTo(s); 
} 
} 
関連する問題