Given a list of integers, sort the values using streams.

sorted() with no arguments sorts the elements of a stream in their natural order, which is ascending for numbers. It returns a new stream, so the original list stays unchanged.

Key Points

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

public class SortValues {

    public static void main(String[] args) {

        List<Integer> list = Arrays.asList(10, 15, 8, 49, 25, 98, 98, 32, 15);

        List<Integer> sorted = list.stream()
                                   .sorted()
                                   .collect(Collectors.toList());

        System.out.println(sorted);
        System.out.println(list);   // original order is unchanged
    }
}

Output

[8, 10, 15, 15, 25, 32, 49, 98, 98]

[10, 15, 8, 49, 25, 98, 98, 32, 15]

Good to Know

Mention that sorted() keeps duplicates. Add distinct() before it if the interviewer wants unique sorted values. For objects, use sorted(Comparator.comparing(Employee::getSalary)).

Interview Tip

A concise interview answer is

I call stream().sorted(), which sorts in natural ascending order and returns a new stream, then collect it into a list. The original list stays unchanged; if I want to sort in place I use list.sort(null) instead.

Last updated: 11 November 2026

Previous question: Given a string, find the first repeated character using streams.

Next question: Given a list of integers, sort the values in descending order using streams.

More Stream API Coding Questions - Level II interview questions

All Stream API Coding Questions - Level II questions