Search...
FAQs
Subscribe
Pie
FAQs
Recent topics
Flagged topics
Hot topics
Best topics
Search...
Search within OCPJP
Search Coderanch
Advance search
Google search
Register / Login
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
Forum:
Programmer Certification (OCPJP)
Sorting objects using comparator doesnt work
Vs Kommuru
Greenhorn
Posts: 7
posted 13 years ago
Number of slices to send:
Optional 'thank-you' note:
Send
Hi,
I would like to sort a list of objects using Comparator. It does nt work. please let me the problem in the following code:
import java.util.*; class DVDInfo { String Name,genre; DVDInfo(String s,String t) { this.Name = s; this.genre = t; } String getName() { return this.Name; } String getGenre() { return this.genre; } } class GenreSort implements Comparator<DVDInfo> { public int compare(DVDInfo one,DVDInfo two) { return (one.getGenre()).compareTo(two.getGenre()); } } class GenSort implements Comparator<DVDInfo> { public int compare(DVDInfo one,DVDInfo two) { return (one.getName()).compareTo(two.getName()); } } public class TestDVD { ArrayList <DVDInfo> dvdlist = new ArrayList <DVDInfo>(); public static void main(String [] args) { new TestDVD().go(); } public void go() { dvdlist.add(new DVDInfo("venk","a")); dvdlist.add(new DVDInfo("govi","b")); dvdlist.add(new DVDInfo("Sree","c")); dvdlist.add(new DVDInfo("sam","d")); GenreSort g = new GenreSort(); Collections.sort(dvdlist,g); for(DVDInfo d:dvdlist) System.out.println(d.getName() + " " + d.getGenre()); GenreSort gs = new GenreSort(); Collections.sort(dvdlist,gs); for(DVDInfo d:dvdlist) System.out.println(d.getName() + " " + d.getGenre()); } }
Thanks,
kommuru
rogel garcia
Ranch Hand
Posts: 41
posted 13 years ago
Number of slices to send:
Optional 'thank-you' note:
Send
It is working...
Check the type of
g
and
gs
objects in the main function....
SCJP 1.4
Vs Kommuru
Greenhorn
Posts: 7
posted 13 years ago
Number of slices to send:
Optional 'thank-you' note:
Send
Thanks Rogel. Its working now.
my overalls have superpowers - they repel people who think fashion is important. Tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
reply
Bookmark Topic
Watch Topic
New Topic
Boost this thread!
Similar Threads
Sorting Collections Doubt
Reading data from a file and storing into array of strings problem
Confused about use of compareTo()
Collections.sort() on Array List
collection problem?? please solve
More...