Mam ListView, który pokazuje informacje o wszystkich klientach ja sortujących klienci nazwę za pomocą tej niestandardowej klasy porównawczej. Oni mają jakiś dodatkowy lerret oprócz liter angielskich który zarządzam z tym setStrength (Collator.SECONDARY)
public class CustomNameComparator implements Comparator<ClientInfo> {
@Override
public int compare(ClientInfo o1, ClientInfo o2) {
Locale locale=Locale.getDefault();
Collator collator = Collator.getInstance(locale);
collator.setStrength(Collator.SECONDARY);
return collator.compare(o1.title, o2.title);
}
}
PRIMARY strength: Typically, this is used to denote differences between base characters (for example, "a" < "b"). It is the strongest difference. For example, dictionaries are divided into different sections by base character.
SECONDARY strength: Accents in the characters are considered secondary differences (for example, "as" < "às" < "at"). Other differences between letters can also be considered secondary differences, depending on the language. A secondary difference is ignored when there is a primary difference anywhere in the strings.
TERTIARY strength: Upper and lower case differences in characters are distinguished at tertiary strength (for example, "ao" < "Ao" < "aò"). In addition, a variant of a letter differs from the base form on the tertiary strength (such as "A" and "Ⓐ"). Another example is the difference between large and small Kana. A tertiary difference is ignored when there is a primary or secondary difference anywhere in the strings.
IDENTICAL strength: When all other strengths are equal, the IDENTICAL strength is used as a tiebreaker. The Unicode code point values of the NFD form of each string are compared, just in case there is no difference. For example, Hebrew cantellation marks are only distinguished at this strength. This strength should be used sparingly, as only code point value differences between two strings are an extremely rare occurrence. Using this strength substantially decreases the performance for both comparison and collation key generation APIs. This strength also increases the size of the collation key.
**Here is a another way to make a rule base sorting if u need it just sharing**
/* String rules="< å,Å< ä,Ä< a,A< b,B< c,C< d,D< é< e,E< f,F< g,G< h,H< ï< i,I"+"< j,J< k,K< l,L< m,M< n,N< ö,Ö< o,O< p,P< q,Q< r,R"+"< s,S< t,T< ü< u,U< v,V< w,W< x,X< y,Y< z,Z";
RuleBasedCollator rbc = null;
try {
rbc = new RuleBasedCollator(rules);
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
String myTitles[]={o1.title,o2.title};
Collections.sort(Arrays.asList(myTitles), rbc);*/
„Czy mogę używać Collections.sort() z jakiegoś komparatora? "Tak, brzmi jak dobry pomysł. – Kennet
Nie wiem, czy to ma znaczenie, ale liczba obiektów na liście będzie wynosiła nawet 80. Dlatego jestem w pewien sposób zdezorientowany, jeśli chodzi o używanie komparatora, jeśli to jest droga, ponieważ to tylko porównuje dwie wartości naraz: –
Tak działa sortowanie Najpierw dodaj jeden element do listy Dodając następny, czy powinien on iść przed czy po bieżącym na liście Kiedy dodajesz trzeci element porównujesz do pierwszego elementu na liście, jeśli po tym czasie porównaj do następnego elementu itd. – Kennet