This week's book giveaways are in the Refactoring and Agile forums.
We're giving away four copies each of Re-engineering Legacy Software and Docker in Action and have the authors on-line!
See this thread and this one for details.
Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

using comparator with array of primitives ?

 
Mohit G Gupta
Ranch Hand
Posts: 634
Chrome Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

why Comparator cannot be used to sort a primitive array ?

why Comparators cannot be used when searching arrays of primitives ?
 
Deepak Bala
Bartender
Posts: 6663
5
Firefox Browser Linux MyEclipse IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Primitives cannot be compared with a Comparator since the comparison is between typed objects. Primitives are not objects
 
Christophe Verré
Sheriff
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Did you try to see what happens ?
 
Shanky Sohar
Ranch Hand
Posts: 1051
Eclipse IDE Firefox Browser
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
mohitkumar gupta wrote:
why Comparator cannot be used to sort a primitive array ?

why Comparators cannot be used when searching arrays of primitives ?



primitive type inbuild have the compare method implemented.............
so there is no need to do that
 
Deepak Bala
Bartender
Posts: 6663
5
Firefox Browser Linux MyEclipse IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Primitives do not have methods
 
Shanky Sohar
Ranch Hand
Posts: 1051
Eclipse IDE Firefox Browser
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Deepak Bala wrote:Primitives do not have methods





yes....deepak you are right..............sorry guys................
the fact is that why primitives doesnot required comparator is.............
because Array.sort() method is overloaded millions times to provide a couple of methods to sort the primitives.............

so there is no need to sort() primitives and if you are trying to do that you will get exception...(i am not sure which error may be compile time or runtime......but most probably you will get runtime exception because sort is done at runtime.)


above explaination which i give is for string class................it implements comparable interface so there is no need to sort it.......
it automatically do that in increasing order.............
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic