我有一个 List<Size> list1
来自 parameters.getSupportedPreviewSizes()
我想按升序排序(width * height)
.
我试过Collection.sort
, 但似乎不起作用。
排序列表的最佳方法是什么?谢谢。
最佳答案
使用自定义 Comparator
.
A
Comparator
is used to compare two objects to determine their ordering with respect to each other. On a givenCollection
, aComparator
can be used to obtain a sortedCollection
which is totally ordered.
final Camera camera = ...;
final List<Camera.Size> sizes = camera.getParameters().getSupportedPreviewSizes();
Collections.sort(sizes, new Comparator<Camera.Size>() {
public int compare(final Camera.Size a, final Camera.Size b) {
return a.width * a.height - b.width * b.height;
}
});
关于android - 如何在 Android 中对 List<Size> 进行排序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12470227/