• Post Reply Bookmark Topic Watch Topic
  • New Topic

sorting an array  RSS feed

 
Nattakan Lukkanapinit
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 ?

 
Dhan Kumar
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.
 
Shashank Agarwal
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.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!