-5

How can I write: (javascript)

array.sort((a, b) => a - b)

To Java? -- I've already tried:

Arrays.sort(array, (a, b) -> a - b);

but I get an error says no suitable method found.

C. Nemeth
  • 11
  • 6
  • 2
    Arrays.sort(array). Here's the documentation. Read it. That's what it's for: https://docs.oracle.com/javase/8/docs/api/java/util/Arrays.html – JB Nizet Oct 28 '18 at 17:21
  • 1
    besides, doing a `minus` for comparator implementation hides bugs for edge cases. don't do that. there is `Integer::compare` or `Comparator.reverseOrder()` for example – Eugene Oct 28 '18 at 17:24
  • What are you guys talking about?? Every time I ask a question on this site all I get is "heres the documentation" Of course I've already looked through that... Whats the point of this site anyways? – C. Nemeth Oct 28 '18 at 17:29
  • 3
    let's suppose you indeed read it, that's fine. Now, *what* is `array`? is it an array of primitives, an array of Objects? what exact code did you try and it failed? and if it did fail with what message. this site is about programming questions (clear) ones - yours is by far not a clear one – Eugene Oct 28 '18 at 17:34
  • I would assume that if there is a difference that the only possible way made sense it would be an integer?? And given that this would only be a translation from one language to another that there would be no need for what error occurred or even what it does just that heres the given and now translate. – C. Nemeth Oct 28 '18 at 17:54

2 Answers2

0

Not very good with javascript, yet searching for how javascript sorts elements, your code

array.sort((a, b) => a - b)

seems to be sorting an array in an ascending order, at the same time coincidently looking at another question from you, the code equivalent for that kind of sorting in java would be

public static int[] smallestToLargestArrangement(int[] arr) {
     Arrays.sort(arr);  // sorts ascendingly
     return arr;
}

Note:- I tried relating the context of your trials to come up with that method, the general approach would be to inline the sort call as and when required.

Naman
  • 27,789
  • 26
  • 218
  • 353
  • Yes, this would be equivalent when ran but is not exactly the same... In javascript you can just use `arr.sort()` in Java you use `Arrays.sort(arr)` but to answer the question... I need `Arrays.sort(arr, and then some comparator function equivalent to javascript )` . You see the reason for doing this is in javascript array.sort() takes a compareFunction which will iterate through each item (a,b) and compare, so that you can have a custom sort. – C. Nemeth Oct 28 '18 at 18:36
  • @C.Nemeth then you already have your answer in the link to your another question which is marked as a duplicate. Isn't it? – Naman Oct 28 '18 at 19:04
-1

Well After a couple days and about 12 hours of working I managed to figure it out on my own... Thanks ;-)

import java.util.Arrays;
import org.apache.commons.lang3.ArrayUtils;
import org.apache.commons.lang3.StringUtils;


public class Solution {
   public static int largestArrangement(int[] arr) {
      return Integer.parseInt(StringUtils.join(Arrays.stream(arr).boxed().sorted((a, b) -> Integer.parseInt(""+b+a) - Integer.parseInt(""+a+b)).mapToInt(i -> i).mapToObj(String::valueOf).toArray(String[]::new), ""));
   }
}
C. Nemeth
  • 11
  • 6
  • 1
    In other words, your problem does not even remotely match what you have posted in your question. Had your posted a reasonable description of your actual problem, you surely got a solution in far less time. – Holger Oct 29 '18 at 10:25