java 8, Sort list of objects by attribute without custom comparator

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));

As a complement to @JB Nizet's answer, if your attr is nullable,

list.sort(Comparator.comparing(AnObject::getAttr));

may throw a NPE.

If you also want to sort null values, you can consider

    list.sort(Comparator.comparing(a -> a.attr, Comparator.nullsFirst(Comparator.naturalOrder())));

or

    list.sort(Comparator.comparing(a -> a.attr, Comparator.nullsLast(Comparator.naturalOrder())));

which will put nulls first or last.