473,405 Members | 2,415 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,405 software developers and data experts.

Problems with comparable interface

I got this code form "Head First Java".

Expand|Select|Wrap|Line Numbers
  1. class Song implements Comparable<Song>{  
  2. String art;  
  3. String tit;  
  4. Song(String a,String t)  
  5. {  
  6. tit=t;  
  7. art=a;  
  8. }  
  9. public int compareTo(Song s)  
  10. {  
  11. return title.compareTo(s.getTitle());  
  12. }  
  13. String getTitile()  
  14. {}  
  15. String getAuthor()  
  16. {}  
  17. }  
  18. class Songsort{  
  19. Arraylist <Song> sl=new Arraylist<Song>();  
  20. public static void main(String arg[])  
  21. {new Songsort.go();  
  22. }  
  23. void go(){  
  24. getSong();  
  25. Collections.sort(sl);}  
  26. }  
  27. void getSong(){  
  28. //..i/o code  
  29. }  
  30. }  
Here we are adding the song objects in the arraylist.Now ,while sorting we are calling sort() method where as a parameter we are passing the entire arraylist of song objects.But how does sort() call compareTo() on a particular song object?And how does
return title.compareTo(s.getTitle());
work? which song objects title are we comparing with s.getTtitle();??
Apr 8 '11 #1
3 2275
Dheeraj Joshi
1,123 Expert 1GB
From Java Doc
The sorting of object arrays is a little more involved, as the compiler doesn't check everything for you. If the object in the array implements Comparable, then you can just sort the array directly, in whole or in part. Otherwise, you must provide a Comparator to do the sorting for you. You can also provide a Comparator implementation if you don't like the default ordering.
When you do
Expand|Select|Wrap|Line Numbers
  1. Collections.sort(Object[])
Is calls the default compareTo method. But in your case you have override the method in song class. So override method will be called during sorting.

Please refer comparable interface document.

Regards
Dheeraj Joshi
Apr 8 '11 #2
@ Dheeraj thanks for the reply..But I still have confusions..When I call sort() using arraylist of song objects ..the control transfers to compareTo()..now, the comparison ->this.title.compareTo(s.getTitle())..in this line is it that each song object is picked up one by one from the list and compared?If yes,then how does the first comparison take place?the first song object is compared to which object?
Sorry for asking stupid questions..
Apr 8 '11 #3
Dheeraj Joshi
1,123 Expert 1GB
Yes each object must be compared with rest of the object.

Java's internal sort algorithm is modified merge sort.

Regards
Dheeraj Joshi
Apr 8 '11 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

1
by: Chris Roe | last post by:
We have run into a problem with a few of our clients running Intel Pentium 4 (no hyperthreading) processors. Our web application under tomcat runs 20 - 30% slower on Intel (benchmarks scores are...
9
by: Pierre Barbier de Reuille | last post by:
Ok, I first want to stress that I looked through the newsgroups archives (on Google) for an answer to my question, but I didn't find it. It's about the interface of the set classes as defined in...
4
by: Greener | last post by:
Hi, I need help badly. Sorry for my ignorance! What's the comparable file in .js to Global.asa in ASP to capture the browser version (IE vs. Netscape)? Any existing code to share or if you have a...
5
by: quartz12h | last post by:
Hi, We tried 2 techniques to mesure db size and both fails to return realtime size. a) under innodb, we show variables, find the innodb_data_file_path variable and parse the size (works...
21
by: Helge Jensen | last post by:
I've got some data that has Set structure, that is membership, insert and delete is fast (O(1), hashing). I can't find a System.Collections interface that matches the operations naturally offered...
4
by: Jim Florence | last post by:
Hello, I've just started on .NET and was asked if there was an easy way to convert a VB.NET app to a web based version without having to re-code the whole thing Many thanks for any pointers ...
0
by: YellowFin Announcements | last post by:
Introduction Usability and relevance have been identified as the major factors preventing mass adoption of Business Intelligence applications. What we have today are traditional BI tools that...
15
by: Xah Lee | last post by:
On Java's Interface Xah Lee, 20050223 In Java the language, there's this a keyword “interface”. In a functional language, a function can be specified by its name and parameter specs....
1
by: ShaveDave27 | last post by:
Hi, I've created a Person Class with a comparable interface. And i've created an ArrayList People with varaibles from the person class in - First_name, Surname, Month(Birthday), Day(Birthday). Now...
4
by: Chris Pateman | last post by:
I have created a number of Views in SQL Server 2005; these are linked to an Access front end. When I run Access queries based on the SQL Views, the Data View shows records that should be filtered out...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.