• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Facing some problem to Sort Genric Student Class

 
Ranch Hand
Posts: 83
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi friends

I created a code as below ,



But when i compile this code it gives an error , as follows



Its not like i didn't understood the error, but than how can i solve it.

So this is my Question

My Question is if i want to sort collection my Student class by using some other class
using comparator, how will i do that ?

my Student can be



or else




Please help me out........
 
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
There are two different problems here. First, your compareTo method assumes it can call compareTo() on the return value of Student.getStudentId(). Since the compiler has no idea what type that method will return, there's an error. To fix this, you can constrain the type parameters for Student by declaring it as



Then the compiler knows that getStudentId() always returns a Comparable. That's one problem. The second one is that you've accidentally made the word "Student" into a type parameter for your SortStudentById class, which is definitely not what you want! Just declare that class as



With these two changes you should be all set.
 
reply
    Bookmark Topic Watch Topic
  • New Topic