Sorting ArrayList with Lambda in Java 8

101,366

Solution 1

For strings this would work

arrayList.sort((p1, p2) -> p1.compareTo(p2));

Solution 2

Are you just sorting Strings? If so, you don't need lambdas; there's no point. You just do

import static java.util.Comparator.*;

list.sort(naturalOrder());

...though if you're sorting objects with a String field, then it makes somewhat more sense:

list.sort(comparing(Foo::getString));

Solution 3

Use list.sort(String::compareToIgnoreCase)

Using list.sort(String::compareTo) or list.sort(Comparator.naturalOrder()) will give incorrect (ie. non-alphabetical) results. It will sort any upper case letter before all lower case letters, so the array ["aAAA","Zzz", "zzz"] gets sorted to ["Zzz", "aAAA", "zzz"]

Solution 4

Suppose you have List of names(String) which you want to sort alphabetically.

List<String> result = names.stream().sorted(
                 Comparator.comparing(n->n.toString())).collect(Collectors.toList());

its working perfectly.

Solution 5

In functional programming, you're not using the old objects to operate on them, but creating the new one in such a fashion:

list.stream().sorted().map(blah-blah).filter(...)...
Share:
101,366

Related videos on Youtube

Jeef
Author by

Jeef

Software Dev

Updated on July 09, 2022

Comments

  • Jeef
    Jeef almost 2 years

    Could somebody show me a quick example how to sort an ArrayList alphabetically in Java 8 using the new lambda syntax.

  • Brian Goetz
    Brian Goetz almost 10 years
    Better: arrayList.sort(String::compareTo)
  • skiwi
    skiwi almost 10 years
    This is incorrect, as now the stream no longer consists of the old elements, but of the mapped and filtered version. Consider a bank account which you want to sort by person name: If you do it like you suggest, then you start of with a stream of bank accounts, and end up with a stream of person names, while you want to end up with a stream of bank accounts again.
  • Dmitry Ginzburg
    Dmitry Ginzburg almost 10 years
    Generally, when you write your programs in FP-style, you don't need to iterately save the results. So, this all is a new list(s): list.stream().sorted() is not sorting the old list, but creating the new one.
  • skiwi
    skiwi almost 10 years
    And that list has just became useless as you only have the person's name left (in my example), and you cannot reference it back (directly) to their bank account anymore.
  • skiwi
    skiwi almost 10 years
    Also, possibly a source of your confusion (nothing to blame), is that the OP requested a method to sort a list, yet you only return an intermediary stream, you also will need to store it at some point, which you omitted from your answer. By trying to implement that, you may see the issue yourself aswell.
  • Dmitry Ginzburg
    Dmitry Ginzburg almost 10 years
    If you really want to store the result in the same list, you can do something like: list = list.stream().sort().collect(Collectors.toList());, but again, author was requesting something in Java 8 style, and that's in Java 8 style.
  • skiwi
    skiwi almost 10 years
    You have now omitted specifying the sort-operator, please first get a working example, then update your answer with the working example.
  • Dmitry Ginzburg
    Dmitry Ginzburg almost 10 years
    @skiwi there's no need to specify the sort operator as OP wanted to sort Strings in lexicographic order, it's the default behavior.
  • Holger
    Holger almost 10 years
  • UTF_or_Death
    UTF_or_Death about 8 years
    Note that both .sort(String::compareTo) and .sort(Comparator.naturalOrder()) will sort all upper case letters before any lower case letters. Usually what you want is .sort(String::compareToIgnoreCase)
  • River
    River over 6 years
    The Comparator is entirely unneeded.
  • Shahzad
    Shahzad over 3 years
    Sticking to functional programming, for (Product p : list) { System.out.println(p.id); } can be written as list.stream().forEach(System.out::println);