Solved

Effective sort in JAVA

Posted on 2001-08-28
7
264 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

 
LVL 19

Expert Comment

by:Jim Cakalic
Comment Utility
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
Comment Utility
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
Comment Utility
Good call. Thanks.
0
 

Expert Comment

by:SpideyMod
Comment Utility
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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
mapAB Challlenge 35 84
java  and programming certification ? 4 63
Problem to start Neon 20 49
backtracking recursion  code 19 39
For customizing the look of your lightweight component and making it look opaque like it was made of plastic.  This tip assumes your component to be of rectangular shape and completely opaque.   (CODE)
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.

772 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

10 Experts available now in Live!

Get 1:1 Help Now