Assuming you actually have a List<AnObject>
, all you need is
list.sort(Comparator.comparing(a -> a.attr));
If you make you code clean by not using public fields, but accessor methods, it becomes even cleaner:
list.sort(Comparator.comparing(AnObject::getAttr));
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…