我当时正在对Java(1.8)中的String列表进行排序,后来发现它不能按预期工作!
我正在尝试以下代码进行排序:
private Set<String> getTestData() {
Set<String> compRoles = new HashSet<>();
compRoles.add("AA");
compRoles.add("Aa");
compRoles.add("aA");
compRoles.add("aa");
compRoles.add("11");
compRoles.add("117");
compRoles.add("12");
compRoles.add("21");
compRoles.add("!@");
compRoles.add("@!");
compRoles.add("@@!");
compRoles.add("BB");
compRoles.add("Bb");
compRoles.add("bb");
return compRoles;
}
public static void main(String args[]) {
List<String> test = new ArrayList<>(new Test().getTestData());
System.out.println(test);
Collections.sort(test);
System.out.println(test);
}
Run Code Online (Sandbox Code Playgroud)
排序前: [AA, Aa, aA, aa, 11, BB, Bb, bb, 12, @!, @@!, 117, 21, !@]
排序后: [!@, 11, 117, 12, 21, @!, @@!, AA, Aa, BB, Bb, aA, aa, bb]
我的期望是: [!@, @!, @@!, 11, 117, …
在 ngFor 中使用 "trackBy: index" 是否是一个好习惯?
例如 :
<li *ngFor="let error of fileErrors.value; trackBy : index;"> {{error}} </li>
Run Code Online (Sandbox Code Playgroud)
在所有 *ngFor 中都需要使用 trackBy 以提高性能,否则我们可以忽略它。
如果我们忽略,那么默认情况下将使用什么迭代器实现?