• Post Reply Bookmark Topic Watch Topic
  • New Topic

Sorting based on the value object in the collection  RSS feed

 
manish ahuja
Ranch Hand
Posts: 312
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi All

I have a requirement where in I have to implement multiple sorting inside a java collection type.
Currently we have not decided on the collection type (List , HashMap etc) but we will go with the one which best assists in support for sorting.

This collection will be populated with a value objects of the same type (i.e. Deal java class).
This Deal java class has following attributes (i am not listing all just the ones used for sorting)
there is java.util.Date tdate
String state
(values could be open, current, new category enabled)
(values could be fail, close, lost category disabled)

So if its enabled it will show only open, current & new
& if its disabled only fail , close & lost

I get the state values in the collection but not the category(enabled/disabled) in the collection as its not a database field.

First of all I would like to know can I sort a java collection type based upon the value of the object the collection holds.

After this sort i would want to sort based on category which i mentioned earlier (enabled/disabled)

If yes do post your suggestions . I am using jdk1.3

Regards
 
Ilja Preuss
author
Sheriff
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can sort a collection based on any criteria you want using a Comparator.

There are collections that automatically sort by insert - all implementations of SortedSet and SortedMap.

List implementations can be sorted on demand by using the utility methods in the Collections class.
 
Naveen K Garg
Ranch Hand
Posts: 105
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Manish,

Please check the following URL:

Generic Comparator for sorting Collections

I have used the same in many of my projects and its cool.

Let me know in case you need more information.

Regards
Naveen K Garg
 
manish ahuja
Ranch Hand
Posts: 312
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Naveen

Thanks for the response.

But the link you mentioned is not working.

Would it be possible for you to post a code snippet using the Comparator interface for sorting

Appreciate your help

Regards
 
Naveen K Garg
Ranch Hand
Posts: 105
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Manish,

Please try following URL:

Generic Comparator

Let me know if your are still not able to check it.

Regards
Naveen K Garg
 
Chetan Raju
Ranch Hand
Posts: 109
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Manish. As per your requirement, since you want sorting, I suggest you use a sorted Collection class like TreeSet.

You can add your own custom class to a Treeset but that class should implement the comparable interface and should have the compare method implemented. Based on the compare method return types (negative, zero or positive values) the sort order is defined.

The following simple class sorts the age in descending order. However, you can customize according to your need.

class MyClass implements Comparable
{
String name;
int age;

MyClass(String name, int age)
{
this.name = name;
this.age = age;
}

public int compareTo(Object obj)
{
if (obj instanceof MyClass)
{
MyClass c = (MyClass) obj;
if (c.age < age)
{
return -1;
} else if(c.age > age) {
return 1;
} else {
return 0;
}
}
return 0;
}
};
 
manish ahuja
Ranch Hand
Posts: 312
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Naveen

That was fantastic. It really helped a lot.

But I have one more scenario in which I want to define my own sort order.

Say in the example you gave which sorts on MAKE
if its ASC it is Dodge, Ford, Toyota & in case of DESC it will be Toyota,Ford,Dodge.
So its a sort on alphabetic order.
To explain my problem in the above example say if my MAKE field can have only one of the three values (Toyota,Ford,Dodge).

But I want to define the sort order . Like for me ASC is Ford, Dodge, Toyota & desc is Toyota, Dodge, Ford.

So is there a way I can superimpose a sort order which is not necessarily in alphabetic order

Regards
 
Ernest Friedman-Hill
author and iconoclast
Sheriff
Posts: 24217
38
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Continued in Java in General (Beginner).
 
Naveen K Garg
Ranch Hand
Posts: 105
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Manish,

Glad to help.

Now regarding your scenario, I guess you will need to write your own sorting logic in the compare and compareTo method of java.util.Comparator class.

Just check the API docs for the same.

Regards
Naveen K Garg
 
manish ahuja
Ranch Hand
Posts: 312
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Naveen

Thanks a lot for your continous responses.

I get what you are trying to say about the Comparator interface & its compare method.
I will give you a brief background about the scenario I am facing. I have fetched the collection of results & set in the web session. Now for every sort i dont want to hit the database so using the database for just sorting data is ruled out.
The data which I have fetched from the database is sorted in the descending date order & i want to retain that.
Within each date I have to sort the data based on the status as I had mentioned in my earlier post.
Like here is the full sort order. The field is status on the basis of which the sort will be carried out.
So my ascending sort is something like this 1) Open 2) Action 3) New 4) Lost 5) Dead. & descending in the opposite direction

Now as this does not follow any alphabetic order I would want to superimpose this order. where O is before A & D as mentioned above.

If you could just give me a way to get started I can take over from there. I am looking for a starting point & till now I tried to find a lot but could not see any sample implementing a custom sort order like my case.

I just want some head start on this like what do i put in my compare method & how do i force to make java understand to consider Open before Action while sorting.


Appreciate your help

Regards
 
Naveen K Garg
Ranch Hand
Posts: 105
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Manish,

As I understood, you already have a collection which sorted. This collection is have various java bean object and each java bean object is having different properties.

Now as per your requirement, you want to achive multiple sorting according to your own rule. Correct?

In this case I guess you will need to have multiple Comparator implements and then using Collections.sort method(List, Comparator) you can achive this.

For more details please refer to http://mindprod.com/jgloss/comparator.html

Now in your case I guess first define a String Array having all the possible values (here status) stored accroding to your sorting order. Then in compare method check each object's status value with the String array and either return <0 or 0 or >0. Then using Collection.sort(List, YourCompartor) you should get your desired results.

I will try to see if I can give you some sample code.

Hope this helps.

Regards
Naveen K Garg
 
manish ahuja
Ranch Hand
Posts: 312
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Naveen

Once again thanks for you prompt help.
Your understanding is almost correct but the collection has objects all of one standard type (something like ValueObject) in the first example you gave.

One more question on what basis should I return 0, <0 or >0.

As you said I am working on the same part of code. Created a class implementing comparator & having the compare method.

I have created a String array with all the status in the sort order (ascending) I want.

Say now say my first object's status value is Open & I match it with the array & Open is the first array element what should I return.
subsequently for other status values

Yea if you could post a sample that would be of great help.
Or if you could throw some light on what action to take on comparing of object's status value with the String array elements that would be of tremendous help


Thanks & Regards
 
manish ahuja
Ranch Hand
Posts: 312
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Naveen

Do post your comments on the same. It would be of great help

Thanks In Advance

Regards
 
Naveen K Garg
Ranch Hand
Posts: 105
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Manish,

I am really sorry, due priority at work I was unable to look into this.

Today also I am little bit busy, but I will see if I can spare some time on this.

Regards
Naveen
 
manish ahuja
Ranch Hand
Posts: 312
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Naveen

Thats very nice of you. I am really sorry to bother you with this.
Inspite of your busy schedule you are taking time out to help people. Thats fantastic.

Do respond whenever you get time

Regards
 
Naveen K Garg
Ranch Hand
Posts: 105
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Manish,

Please check this sample code to see if this is what your are looking.



Please check the compare method. You may need to refine it according to your need.

Please post your feedback.

Regards
Naveen K Garg
 
manish ahuja
Ranch Hand
Posts: 312
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Naveen

That was awesome. It really solves the problem which I am facing.

Thanks a lot for your time & help


Regards
 
Naveen K Garg
Ranch Hand
Posts: 105
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Manish,

Thanks for the feedback.

Regards
Naveen
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!