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:
Windows Errors? Fix Them Before They Spread
Repair common Windows errors and clear accumulated junk for a smoother, more stable PC - no reinstall needed.Free scan · no reinstallOutdated Drivers Are Slowing You Down
One free scan finds every outdated or missing driver and matches the right update for your exact hardware.Free scan · exact hardware matchpersonList.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:
Rank #2
| 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:
Quick wins for a faster PC:
Repair Windows errors before they cause bigger problemsFix Now →Fix the driver behind crashes, sound loss and screen glitchesFind Drivers →Clear out junk files and repair common Windows errorsFree Scan →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:
Rank #4
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:
The Tool Desk
Outbyte PC Repair FREEClear out junk files and repair common Windows errorsFree Scan →Outbyte Driver Updater FREEScan for outdated or missing drivers - takes under a minuteDriver Scan →Best Value
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.
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
compareTodoes 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.
Quick Recap
Sources
- Oracle Java tutorial: Lambda Expressions
- Oracle Java SE 8 API: Comparator
- Oracle Java SE 8 API: List
- Apache NetBeans documentation: JDK 8 support
- Oracle: Lambda expressions in Java 8
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.

