?
Solved

Effective sort in JAVA

Posted on 2001-08-28
7
Medium Priority
?
289 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 200 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
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:
Suggested Courses
Course of the Month17 days, 9 hours left to enroll

830 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