3

I've recently got into functional programming and Java 8 lambdas. I have an array of ints and I want to sort it in an ascending order.

The way I am trying to do this with lambda is as follows:

Arrays.stream(intArray).sorted((x, y) -> Integer.compare(x, y) == -1);

The issue with this is that my compiler says:

Error:(12, 32) java: method sorted in interface 
java.util.stream.IntStream cannot be applied to given types;
required: no arguments
found: (x,y)->Int[...]== -1
reason: actual and formal argument lists differ in length

What am I missing here?

Kedar Mhaswade
  • 4,535
  • 2
  • 25
  • 34
Kobek
  • 1,149
  • 6
  • 18
  • 40
  • 4
    Streams do not offer a way to sort an array, they allow to *process the elements* in a sorted order. The original array is not modified and in your case, since there is no actual operation, nothing will happens at all. To sort an array, use `Arrays.sort(array);` that’ll work for `int[]` and `Integer[]`. – Holger Apr 02 '16 at 08:12

3 Answers3

5

A Comparator takes two Object and returns an int.

Here, we're entering with two Object's but are returning a boolean expression (Integer.compare(x, y) == -1)

You actually just need the first part of it

Arrays.stream(intArray)
      .sorted((x, y) -> Integer.compare(x, y))
      .toArray(Integer[]::new);

Since you seem to be using an array of Integer objects (because an IntStream doesn't have a sorted(Comparator) method) you better be using a simple IntStream, it will simplify your life because the int will than be sorted in their natural order.

IntStream#sorted()

Returns a stream consisting of the elements of this stream in sorted order.

Arrays.stream(intArray)
      .mapToInt(x->x)
      .sorted()
      .toArray();
Yassin Hajaj
  • 21,337
  • 9
  • 51
  • 89
  • That was my original approach. But even doing it this way, it still gives me the "Integer.Compare(x, y)" cannot be applied to , – Kobek Apr 02 '16 at 07:58
  • @Kobek May you please post the full code? With the `intArray`, it'll help reproduce the problem. – Yassin Hajaj Apr 02 '16 at 07:59
  • 3
    Both of these are incomplete; because there is no terminal operation, neither does anything. You want a `toArray()` at the end (or similar.) – Brian Goetz Apr 02 '16 at 21:32
  • @BrianGoetz You were totally right. Is it better now? – Yassin Hajaj Apr 02 '16 at 21:33
  • Since the result is the sorted array returned by `toArray()` you should assign the result to a variable (in the first case, assigning it to the existing `intArray` would be fine, in the second, the result is `int[]` where the source is `Integer[]`)… – Holger Apr 05 '16 at 16:40
  • All these methods are great, but my main problem here is that no matter what I do, every time I call the .sorted((x, y) -> Integer.compare(x, y) I get an error, saying that x and y cannot be compared as they are lambda expressions. For anyone still interested, here is the full code of my program (commented) http://pastebin.com/HXmNUeXx – Kobek Apr 08 '16 at 16:04
  • @Kobek: this has already been explained in [this answer](http://stackoverflow.com/a/36370900/2711488)… – Holger Jun 02 '16 at 18:05
3

You can do

Arrays.stream(intArray).sorted().forEach(System.out::println);  

The error in your case is because IntStream#sorted() does not take parameters.

DEMO

singhakash
  • 7,891
  • 6
  • 31
  • 65
1

Arrays.stream(intArray).sorted() Should be used for processing the array in a specific order. It can be used to sort arrays as explained in other answers but it will generate a new sorted array.

Arrays.stream(intArray)
      .sorted((x, y) -> Integer.compare(x, y))
      .toArray(Integer[]::new);

In case of very big sized data this will not be efficient.

If you want to do in-place sorting, you can use Collections.sort()

Arrays.sort(arr, (x,y) -> Integer.compare(x,y))

Or in the case of objects of type A and sorting based on field P

Arrays.sort(arr, Comparator.comparing(A::getP))

where getP() is the method that gets the field P value.

In case of reversed sorting

 Arrays.sort(arr, Comparator.comparing(A::getP).reversed())

In case of sorting on 2 fields P and P2

Arrays.sort(arr, Comparator.comparing(A::getP).thenComparing(A::getP2))
Mohamed Gad-Elrab
  • 636
  • 1
  • 6
  • 20
  • How to sort a raw array like `int[] arr` with a lambda that has multiple lines (various if elses). Something like this: `Arrays.sort(arr, );` – PlsWork Apr 07 '20 at 15:28