• Post Reply Bookmark Topic Watch Topic
  • New Topic

arrays sort not works with Comparator  RSS feed

 
Andrzej Borucki
Ranch Hand
Posts: 90
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I wanna sort pixels by red,green or blue;



Is error:
The method sort(int[]) in the type Arrays is not applicable for the arguments (int[], new Comparator<int[]>(){})
 
Darryl Burke
Bartender
Posts: 5167
11
Java Netbeans IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The compiler is telling you that Arrays doesn't have a sort method that takes an int[] and a Comparator as parameters. Looking at the documentation, I see that's true.
 
Andrzej Borucki
Ranch Hand
Posts: 90
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Is OK if I change to :

Change "int" to "Integer"
Problem: which is difference in memory between int and Integer? If Integer allocates million objects when I initialize pixels = new Integer[1000000] ?
 
Campbell Ritchie
Marshal
Posts: 56525
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Andrzej Borucki wrote:Is OK if I change to . . .
No.

You do not need anything to tell whether 3 is greater than 4. If you have an int[] to sort you simply ask the sort method to sort it. No Comparaotr or anything.
 
Andrzej Borucki
Ranch Hand
Posts: 90
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Is any method else than Comparator to compare with sort? I must sort by Red or Green or Blue component. If I change int to Integer appears problems with System.arraycopy, array.Clone() etc
 
Paweł Baczyński
Bartender
Posts: 2074
44
Firefox Browser IntelliJ IDE Java Linux Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Am I correct assuming that you have three separate int arrays, each for one base color?
And you want to sort them all according to one of base colors?
Well, I would use Color class then. And I would sort Color[].
 
Andrzej Borucki
Ranch Hand
Posts: 90
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Three colors are on one integer. There are get from
 
Darryl Burke
Bartender
Posts: 5167
11
Java Netbeans IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you're comparing int[]s then your array should be int[][]
 
Andrzej Borucki
Ranch Hand
Posts: 90
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I use
 
Campbell Ritchie
Marshal
Posts: 56525
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Paweł is quite right; you want an array of Colors, not int[] nor byte[]. Color does not implement Comparable, but it has getAlpha getBlue getGreen and getRed methods which can all be used by a Comparator.

And if you tell us you want to sort ints when you actually want to sort Colors, you are going to get unhelpful and confusing answers.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!