Solved

how to sort objects ascending and descending

Posted on 2006-06-21
8
406 Views
Last Modified: 2013-11-23
How would you use comparators to sort objects ascending and descending.

Suppose I want to be able to sort an object by the age field either ascending or descending.
Also I have an object that can be sorted by more than field. You can sort by name and age.
How can I implement more than 1 comparator?

0
Comment
Question by:inzaghi
  • 3
  • 3
  • 2
8 Comments
 
LVL 86

Assisted Solution

by:CEHJ
CEHJ earned 250 total points
ID: 16949833
You can pass the field as a parameter to the ctor of the Comparator. Reverse the comparison for asc.ssec
0
 
LVL 92

Accepted Solution

by:
objects earned 250 total points
ID: 16949836
You would use a different Comparator for each sort order that you needed.
To get the reverse order iuse:

http://java.sun.com/j2se/1.5.0/docs/api/java/util/Collections.html#reverseOrder(java.util.Comparator)
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 16949837
Typo:

>>Reverse the comparison for asc.ssec

should have been

Reverse the comparison for asc/desc
0
Active Directory Webinar

We all know we need to protect and secure our privileges, but where to start? Join Experts Exchange and ManageEngine on Tuesday, April 11, 2017 10:00 AM PDT to learn how to track and secure privileged users in Active Directory.

 

Author Comment

by:inzaghi
ID: 16949893
I am using java 1.4
0
 
LVL 92

Expert Comment

by:objects
ID: 16949928
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 16950065
:-)

You only need one Comparator
0
 

Author Comment

by:inzaghi
ID: 16950270
Alternatively I could have one class which implements the Comparable interface and otehr comparators I can define as annonymous classes

 public static final Comparator AGE_ORDER = new Comparator() {
             public int compare(Object o1, Object o2) {
             }
 };

 public static final Comparator NAME_ORDER = new Comparator() {
             public int compare(Object o1, Object o2) {
             }
};
0
 
LVL 92

Expert Comment

by:objects
ID: 16950328
> Alternatively I could have one class which implements the Comparable interface and otehr comparators I can define as annonymous classes

yes you could use inner classes for your comparators. It would still give you a seperate class for each sort order you needed, ie. one comparator per sort order.

0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

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.

Question has a verified solution.

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

For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.

828 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