Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

In Java 8, sort a List by passing a two-argument lambda to List.sort or Collections.sort. NetBeans can help convert an eligible anonymous Comparator into a lambda, but the sorting syntax is a Java feature, not a special NetBeans API.

Sort a list with a Java 8 lambda

A Comparator supplies the comparison function. Because it is a functional interface, Java can infer the lambda’s parameter types from the method argument:

List<Person> personList = new ArrayList<>();
personList.sort((p1, p2) -> p1.firstName.compareTo(p2.firstName));

This orders the list by firstName using the strings’ natural, case-sensitive ordering. The expression returns a negative value when the first name should come before the second, zero when they compare equally, and a positive value when it should come after.

The lambda can use inferred parameter types, as above, or declare them explicitly:

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
personList.sort((Person p1, Person p2) ->
    p1.firstName.compareTo(p2.firstName));

A single-expression lambda returns its expression’s value implicitly; it does not need a return statement or braces. For a block lambda with multiple statements, use braces and an explicit return.

Use List.sort or Collections.sort

Java 8 provides List.sort as a concise instance method. The equivalent Collections.sort call is also valid when given a comparator:

Approach Example When to use it
List.sort personList.sort((p1, p2) -> p1.firstName.compareTo(p2.firstName)); Prefer this direct form when sorting a list in Java 8 or later.
Collections.sort Collections.sort(personList, (p1, p2) -> p1.firstName.compareTo(p2.firstName)); Use this familiar utility form when it fits the surrounding code or teaching example.

Both forms sort the list in place; neither creates a separately sorted list.

Convert an anonymous Comparator in NetBeans

Before lambdas, the comparator could be written as an anonymous inner class and passed to Collections.sort:

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
Collections.sort(personList, new Comparator<Person>() {
    @Override
    public int compare(Person p1, Person p2) {
        return p1.firstName.compareTo(p2.firstName);
    }
});

After conversion, the comparison logic can be expressed directly as a lambda:

Collections.sort(personList,
    (Person p1, Person p2) -> p1.firstName.compareTo(p2.firstName));

Then remove the parameter types if inference makes the shorter form clear:

Collections.sort(personList,
    (p1, p2) -> p1.firstName.compareTo(p2.firstName));

Finally, use the Java 8 list method if desired:

personList.sort((p1, p2) -> p1.firstName.compareTo(p2.firstName));

NetBeans documents support for JDK 8 features, including lambda expressions, and its editor hints can offer conversion of eligible anonymous inner classes. The available hint and menu labels depend on the IDE version; inspect the editor suggestion for the selected code rather than relying on an old screenshot or menu path. The original NetBeans lambda-sorting walkthrough was published in 2013 and used nightly builds, so its setup directions are historical, not a current installation guide.

Sort by multiple or numeric keys

For a reusable and readable ordering, Java 8’s comparator factory methods express key extraction and tie-breaking without writing the comparison arithmetic yourself:

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
personList.sort(Comparator.comparing(Person::getLastName)
                          .thenComparing(Person::getFirstName));

This sorts by last name first, then by first name when last names compare equally. Comparator.comparing extracts a key that implements Comparable; thenComparing adds another comparison in sequence.

For primitive numeric keys, use a specialized factory such as Comparator.comparingInt or Comparator.comparingLong. This makes the intended numeric comparison explicit and avoids relying on boxed key values.

Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Support on Ko-Fi

Choose comparison rules deliberately

The direct String.compareTo example gives a specific ordering, not a universal one. Before using it with real data, decide what the application expects:

  • Case: natural string ordering distinguishes uppercase and lowercase. If names should compare without regard to case, supply an appropriate key comparator rather than assuming compareTo does so.
  • Locale: user-facing alphabetical order can vary by language and locale. Use a locale-aware comparison policy when that is the requirement.
  • Nulls: decide whether a missing key is invalid, should sort first, or should sort last. Choose and apply a null policy explicitly rather than allowing unexpected null values to fail during comparison.
  • Reuse: an inline lambda suits a one-off sort. If several operations need the same ordering, assign the comparator to a named variable or method so the rule has one clear definition.

What NetBeans support does—and does not—mean

NetBeans provides editor and project support for writing Java 8 code, including highlighting and hints for lambda syntax and eligible refactorings. The lambda itself is part of Java 8, and the compiler’s target type is the Comparator; no NetBeans-specific sorting API is involved. Check the JDK and language level configured for the project if the IDE marks lambda syntax as an error.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Sources

Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.