This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes sorting by a primitive int field Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "sorting by a primitive int field" Watch "sorting by a primitive int field" New topic

sorting by a primitive int field

saima kanwal
Ranch Hand

Joined: Oct 05, 2009
Posts: 148
I am trying to sort my array list using comparator. please review my code and solve my problem. I want to sort my array list by int type isbn number of book. My code is as follows:

And it does not compile because I am using the compare to method with int. How am i going to compare int (primitive)? please modify my code.


SCJP 6.0 (86%) SCWCD 5 (94%)
Christophe Verré

Joined: Nov 24, 2005
Posts: 14687

Hint: The Integer class is comparable

[My Blog]
All roads lead to JavaRanch
Ankit Garg

Joined: Aug 03, 2008
Posts: 9291

In the above statement, isbn is a primitive so you can't call compareTo method on it. But since it is a primitive, you can use comparison operator on it. So if you see the source code of Integer class, the compareTo method is implemented like this

You can implement your compareTo method in the same way too. To make it even simpler, you can just write

SCJP 6 | SCWCD 5 | Javaranch SCJP FAQ | SCWCD Links
Consider Paul's rocket mass heater.
subject: sorting by a primitive int field
Similar Threads
newbee cant find string
Confused. How do I add links to each item in a list, how to get JSF to know which link was clicked?
Assertions doubt?
comparing Boolean wrappers objects
Print Object output incorrect - Head First Java Chapter 16 TreeSet Exercise