数组根据某一个字段进行排序
实际使用
myHeadphonesList.sort(((a, b) => (b.toneQuality ?? 0).compareTo(a.toneQuality ?? 0)));
例子
List myList = [
{'name': 'xiaom', 'age': 20},
{'name': 'xiaoj', 'age': 22},
{'name': 'xiaoh', 'age': 16},
];
myList.sort(((a, b) => b['age'].compareTo(a['age'])));
print('$myList');
// [{name: xiaoj, age: 22}, {name: xiaom, age: 20}, {name: xiaoh, age: 16}]
倒序
实际使用
myHeadphonesList.reversed.toList();
例子
List myList = [
{'name': 'xiaom', 'age': 20},
{'name': 'xiaoj', 'age': 22},
{'name': 'xiaoh', 'age': 16},
];
myList.reversed.toList();
print('$myList');
// [ {name: xiaoh, age: 16}, {name: xiaoj, age: 22},{name: xiaom, age: 20}]
List.reversed就是倒序了,但是这样之后就变成了Iterable
的类型,所以需要toList()
一下.