假設我有一個人員列表,我需要先按Age分類然後按名稱排序。在Kotlin中對多個字段進行排序
從C#-background的到來,我可以很容易地做到這一點的說,通過使用LINQ語言:
var list=new List<Person>();
list.Add(new Person(25, "Tom"));
list.Add(new Person(25, "Dave"));
list.Add(new Person(20, "Kate"));
list.Add(new Person(20, "Alice"));
//will produce: Alice, Kate, Dave, Tom
var sortedList=list.OrderBy(person => person.Age).ThenBy(person => person.Name).ToList();
怎樣才能做到這一點使用科特林?
這是我試過
val sortedList = ArrayList(list.sortedBy { it.age }.sortedBy { it.name })) //wrong
Kotlin是天賜之物 – ElliotM