Solved

Effective sort in JAVA

Posted on 2001-08-28
7
265 Views
Last Modified: 2008-02-01
What is the most effective way of sorting objects in a vector, based on a field in the object
0
Comment
Question by:bharathr
7 Comments
 
LVL 6

Accepted Solution

by:
kotan earned 50 total points
ID: 6432083
1. Let that objects implement java.lang.Comparable
2. Implement public int compareTo(Object o)
3. Call
        Collections.sort(l);
      l = vector.


Example of the compareTo() implementation,

public class Name implements Comparable {
              private String  firstName, lastName;

              public int compareTo(Object o) {
                  Name n = (Name)o;
                  int lastCmp = lastName.compareTo(n.lastName);
                  return (lastCmp!=0 ? lastCmp :
                          firstName.compareTo(n.firstName));
              }
          }

0
 
LVL 1

Expert Comment

by:wgilster
ID: 6432115
First off, there is a superb generic sorting implementation here in devx.  Download that.
Then make a class that extends your base class and have it implement the java.util.Comparator interface (all you need to override is the equals and compareTo methods for that field you spoke of).  You could call this class MyFieldNameIndex.  Link in the class you downloaded from devx, and if you use one of their sort algorithms, your done!
0
 

Expert Comment

by:yoavdo
ID: 6433099
U can use the ObjectSpace jar: jgl (it is free). main concept is to define a class that implemnts the BinaryPredicate : implemnt an excute method which determine which of the two Objects it recives is bigger, U can define it as U want. Then convert your Vector to an VectorArray (a data type of jgl) and call a static method that gets your VectorArray and the predicate U defined. it sorts the VectorArray U gave. The sort method is 'QuickSort' the preformence is good. If U want a detailed example, make a comment.
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 19

Expert Comment

by:Jim Cakalic
ID: 6433762
As indicated in previous posts, the java.util.Collections class implements a mergesort algorithm available through its sort methods. kotan suggested that the objects placed in the collection implement Comparable. This typically works for a homogeneous collection because the sort method demands that "all elements in the list must be mutually comparable (that is, e1.compareTo(e2) must not throw a ClassCastException for any elements e1 and e2 in the list)." This isn't a problem when all objects in the collection are of the same type. But what to do when the collection is heterogeneous?

Collections provides an alternate sort method which takes a List and a Comparator object. A Comparator object implements the ordering function which is to be applied regardless of whether the object types are themselves Comparables and can better deal with heterogeneous collections because you can code it to expect objects of any type appropriate to your application.

The following JDC articles might be of assistance. The first is about Collections with a short segment on comparing and the second described Comparator and Comparable in more detail with respect to arrays of Objects. The principle is the same for collections.

http://developer.java.sun.com/developer/technicalArticles/Collections/Using/#co
http://developer.java.sun.com/developer/TechTips/1999/tt0923.html#tip2

Best regards,
Jim Cakalic
0
 
LVL 5

Expert Comment

by:vemul
ID: 7755806
No comment has been added lately, so it's time to clean up this TA.
I will leave a recommendation in the Cleanup topic area that this question is:

- split points between kotan and jim_cakalic

Please leave any comments here within the
next seven days.

PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER !

vemul
Cleanup Volunteer
0
 
LVL 19

Expert Comment

by:Jim Cakalic
ID: 7764381
Good call. Thanks.
0
 

Expert Comment

by:SpideyMod
ID: 7847140
per recommendation

SpideyMod
Community Support Moderator @Experts Exchange

jim_cakalic points for you at:
http://www.experts-exchange.com/Programming/Programming_Languages/Java/Q_20489591.html
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Free Alternative to JIRA 4 90
eclipse formatting 6 70
rhino JavaScript import, load 25 66
why cannot we forward request once the response is committed 2 33
For customizing the look of your lightweight component and making it look lucid like it was made of glass. Or: how to make your component more Apple-ish ;) This tip assumes your component to be of rectangular shape and completely opaque. (COD…
Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
Viewers learn about the “while” loop and how to utilize it correctly in Java. Additionally, viewers begin exploring how to include conditional statements within a while loop and avoid an endless loop. Define While Loop: Basic Example: Explanatio…
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.

930 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now