| Author |
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.
saima.
|
SCJP 6.0 (86%) SCWCD 5 (94%)
|
 |
Christophe Verré
Sheriff
Joined: Nov 24, 2005
Posts: 14672
|
|
|
Hint: The Integer class is comparable
|
[My Blog]
All roads lead to JavaRanch
|
 |
Ankit Garg
Saloon Keeper
Joined: Aug 03, 2008
Posts: 9189
|
|
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
|
 |
 |
|
|
subject: sorting by a primitive int field
|
|
|