• 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
  • Liutauras Vilda
  • Paul Clapham
  • Bear Bibeault
  • Jeanne Boyarsky
Sheriffs:
  • Ron McLeod
  • Tim Cooke
  • Devaka Cooray
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Jj Roberts
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • salvin francis
  • Scott Selikoff
  • fred rosenberger

sorting an array

 
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is mycode sofar.
I already figure it out how to sort by name but i can't sort by price because compareTo don't accept int.
Any suggestion ?

 
Greenhorn
Posts: 29
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

I already figure it out how to sort by name but i can't sort by price because compareTo don't accept int.
Any suggestion ?



your following code does the same as you want...



Please tell me if I made a mistake here...
 
Dhan Kumar
Greenhorn
Posts: 29
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Also I will be surprised it this line works and not give compilation error.

if(this.price < tmp.price)



your compareTo method is static ..so you cant use this.price in static context.
 
Ranch Hand
Posts: 105
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm a little confused. It seems that your compareTo is already sorting it by price, not name.
 
Nattakan Lukkanapinit
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
here is my original code that work with name


but when i change address to price in

It doesn't accept int into it.
 
Dhan Kumar
Greenhorn
Posts: 29
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In the first case .. compareTo of object String is called. This will not be applicable for int which is primitive.

Try this code..It will work with some minor modification.

 
Nattakan Lukkanapinit
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you so much.
 
If you settle for what they are giving you, you deserve what you get. Fight for this tiny ad!
Thread Boost feature
https://coderanch.com/t/674455/Thread-Boost-feature
reply
    Bookmark Topic Watch Topic
  • New Topic