• Post Reply Bookmark Topic Watch Topic
  • New Topic

How To Sort Array In Decreasing Order  RSS feed

 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I Tried below code for sorting integer in decreasing order and It Worked

but when I Write int [] arrayToSort = {5,89,16,2};
it throws error can someone explain whats going on ?
thanks in advance
 
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rupesh Painkra wrote:but when I Write int [] arrayToSort = {5,89,16,2};
it throws error can someone explain whats going on ?

Sure, if you look in Collections.reverseOrder(), you'll notice that it specifies a <T> argument, which is "the class of the objects compared by the comparator".

Unfortunately, Java generics doesn't (as yet) allow you to substitute a primitive for a class; and int is a primitive.

Winston
 
Marshal
Posts: 56600
172
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch

You know you can turn an array into a Stream; you can actually get an IntStream out of an int[] array
int[] sortedBackwards = IntStream.of(myArray) // won't compile yet.
You can sort an IntStream but you cannot reverse it nor sort it backwards, but you might be able to do that if you create a Stream<Integer>. If you go through the IntStream documentation you find a method which boxes all its elements and you get a Stream<Integer>.
int[] sortedBackwards = IntStream.of(myArray)
      .boxed() // still won't compile

You find a Stream has a sorting method, but that takes natural ordering -999, 1, 2, 3, 4, 5 ... and there doesn't appear to be a reverse method. There is another sorting method which takes a Comparator as its argument. Since Integer implements Comparable<Integer> it has a compareTo() method which you can use; if you simply use i and j in the wrong order it will sort backwards. You can use a λ and pass i, j then -> then the method call, as in blue writing below.
int[] sortedBackwards = IntStream.of(myArray)
      .boxed()
      .sorted((i, j) -> j.compareTo(i)) // still won't compile Integer.compare(j, i) would work too.

Now, you can get arrays out of Streams, but you have to pas a reference to an Integer[] constructor which I think you can do by writing Integer[]::new. I couldn't work out how to create an int[]. I think you might have to do some casting.
int[] sortedBackwards = IntStream.of(myArray)
      .boxed()
      .sorted((i, j) -> j.compareTo(i))
      .toArray(Integer[]::new); // In the immortal words of Homer Simpson, Woohoo!

If you are starting from an Integer[] there is a simpler way to do that:-
Integer[] sortedBackwards = Stream.of(myArray)
      .sorted((i, j) -> j.compareTo(i))
      .toArray(Integer[]::new);
 
Ranch Hand
Posts: 934
4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
depends upon type of input is:

If i assume all numbers are positive and non repetitive and also you know what is the largest number is then you can do than very fast and
by using very less memory. Practically it is O(n).

here is pseudo code:




 
Campbell Ritchie
Marshal
Posts: 56600
172
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I see I wrote int[] = several times even after I realised I could only produce an Integer[] like that.

Clever idea about a BitSet, but you have the drawback you get with any Set: it does not support duplicates. What if you wanted to sort this?
int [] arrayToSort = {5, 89, 16, 2, 5};
 
Campbell Ritchie
Marshal
Posts: 56600
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
As I said earlier, I was mistaken writing
int[]
when I was creating Integer[] arrays. It is awkward swapping back and forth between reference types and primitives, even more so with arrays or primitives. There are all sorts of things I wish they had done differently or earlier (e.g. introducing generics from day 1), but I think this is the first time I have thought I wish they had got rid of primitives.
 
Tushar Goel
Ranch Hand
Posts: 934
4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:
Clever idea about a BitSet, but you have the drawback you get with any Set: it does not support duplicates. What if you wanted to sort this?
int [] arrayToSort = {5, 89, 16, 2, 5};


Yeah we have assumed that numbers are duplicates. For the array you have given same procedure applied as i mentioned early.
 
Ranch Hand
Posts: 297
Eclipse IDE Firefox Browser Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I really hope that they could have provided a flag in Arrays.sort() but how about creating your own method by reversing logic of any of Arrays.sort() method? All you need to change is the swapping logic, isn't it?
 
Sheriff
Posts: 11494
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Javin Paul wrote: I really hope that they could have provided a flag in Arrays.sort() but how about creating your own method by reversing logic of any of Arrays.sort() method? All you need to change is the swapping logic, isn't it?

By "provided a flag" I take it you mean a "sort in reverse" flag as part of the parameter list? In general, that's not a good design choice (see antipattern: passing a flag). Passing in a Comparator as Campbell demonstrated in his example is a much more flexible solution and one which includes sorting in reverse as an option among many other ways you can choose to sort. You basically pass in a "sorting strategy" that is applied without having to change the looping/iterating logic.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!