This week's giveaway is in the JDBC forum.
We're giving away four copies of Java Database Connections & Transactions (e-book only) and have Marco Behler on-line!
See this thread for details.
Win a copy of Java Database Connections & Transactions (e-book only) this week in the JDBC forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Devaka Cooray
  • Knute Snortum
  • Paul Clapham
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Bear Bibeault
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Ron McLeod
  • Piet Souris
  • Frits Walraven
Bartenders:
  • Ganesh Patekar
  • Tim Holloway
  • salvin francis

Sorting on int field of object  RSS feed

 
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The following code works fine for sorting on the title (String) field of the object Song. But if I want to sort on the intRating (int) field and I change line #53 to "return intRating.compareTo(s.getIntRating());" I get and error in NetBeans saying "int cannot be dereferenced." How can I go about sorting on this int field?

Thank you for your help.



 
Rancher
Posts: 618
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
intRating is an int, which is a primitive type. The C# language allows you to call methods on primitive types but Java does not. You need to change your code to use the syntax that compares two primitive types.
 
Ranch Hand
Posts: 607
Firefox Browser Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Tom Reilly wrote:intRating is an int, which is a primitive type. The C# language allows you to call methods on primitive types but Java does not. You need to change your code to use the syntax that compares two primitive types.



Or change the int to an wrapped Integer type ...
 
Bud Tippins
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you for your help. That worked.
 
Sheriff
Posts: 21747
102
Chrome Eclipse IDE Java Spring Ubuntu VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Tom Reilly wrote:intRating is an int, which is a primitive type. The C# language allows you to call methods on primitive types but Java does not. You need to change your code to use the syntax that compares two primitive types.


Just a warning: don't use simple subtraction to compare the two. That will give you problems when overflow occurs. Consider the following example:
That's why I always use < and >, usually in a nested terniary operator for brevity:
After all, the comparison only cares if the value is 0, negative or positive. The actual value does not matter.

Of course, since the result of any mathematical operation is never smaller than int, you can simply use subtraction for bytes, chars and shorts:
 
Hey cool! They got a blimp! But I have a tiny ad:
how do I do my own kindle-like thing - without amazon
https://coderanch.com/t/711421/engineering/kindle-amazon
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!