Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

Effective sort in JAVA

Posted on 2001-08-28
7
267 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
Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

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…
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…
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.

840 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