Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Please help with sorting multiple columns

 
jay lai
Ranch Hand
Posts: 180
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have the similar problem with this post
sort

I have a method to do the sort, the whole list is in ArrayList, and I need to sort first column first , and then if the second columns:

For example: sorted by name and within name of 1st column sorted by State
Name | State
--------------
Adam | Arizona
Adam | Vermon
Zoo | Maryland
Zoo | New England
Zoo | Virginia

I got things works only sorted on the first columns ONLY?
Can any one help me how to sorted the seconde one and the first one still be sorted too...?j

Many thanks in advance

 
Joel McNary
Bartender
Posts: 1840
Eclipse IDE Java Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The Collections.sort method is "is guaranteed to be stable: equal elements will not be reordered as a result of the sort." (From the javadoc).

What does this mean? To do what you want to do, first sort by the least significant column and then work your way to the most significant. In this case, sort by State first and then Name.

For example, the input:

Zoo | New England
Adam | Arizona
Zoo | Maryland
Adam | Vermont
Zoo | Virginia

When sorted by State:

Adam | Arizona
Zoo | Maryland
Zoo | New England
Adam | Vermont
Zoo | Virginia

And then by Name:

Adam | Arizona
Adam | Vermont
Zoo | Maryland
Zoo | New England
Zoo | Virginia

The relative positions of Maryland, New England, and Virginia are not changed because this is a stable sort.
 
jay lai
Ranch Hand
Posts: 180
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Joel McNary ,
Can you giving more detail, in term of coding, for example: I just keep the way I do in MySort class, and just calling it twice, First path: passing the getState, then second path : passing getName?

MySort(listresult, "state", true)
MySort(listresult, "name", true)?

thank you in advance
 
jay lai
Ranch Hand
Posts: 180
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can anyone please help, this is urgent...
Greatly appreciated.
 
Burkhard Hassel
Ranch Hand
Posts: 1274
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

you'll need two comparators, one for the name and one for the state.

See java.util.Comparator in the API.

Then:
Collections.sort(list, new StateComparator());
Collections.sort(list, new NameComparator());


Yours,
Bu.
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can you modify MySort? What if it had a constructor like

Comparators often do something like

Your generic MySort could loop through any number of methods or fields until it gets a non-zero result. Java5 supports "varargs" syntax, so you wouldn't even break existing code by accepting an array of method names.
 
jay lai
Ranch Hand
Posts: 180
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Stan: so are you saying that i would do something like this, So what do I do with the Compare method? Thank you in advance

 
steve souza
Ranch Hand
Posts: 862
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I did something similar with my open source project www.fdsapi.com. I allow you to write sql against Object[][] and I needed this capability to support the order by clause. With slight modifications this should work for you, or you could simply make an Object[][] at of your data and you wouldn't have to write this code. fyi Your code only sorts by one column at a time.

Example:
select * from array order by col1 desc, col4 asc, col8 asc

Here is the class that does this. Primarily focus on the compare method. I have a list of Comparators I loop through. To see sample usage code look in the main method.

http://fdsapi.cvs.sourceforge.net/fdsapi/fdsapi/Code/com/fdsapi/arrays/ArrayComparator.java?revision=1.12&view=markup
[ July 01, 2007: Message edited by: steve souza ]
 
jay lai
Ranch Hand
Posts: 180
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Steve
I am going to look into it,
Greatly appreciated for your sharing and helping.
Regards
 
jay lai
Ranch Hand
Posts: 180
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Steve:
After look into the ArrayComparator , I would create a ArrayComparator.java in Utils, and from my caller class:


Please let me know if I am on the right track by using your class.
Tons of thanks in advance

 
steve souza
Ranch Hand
Posts: 862
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You just seem like you have copied my code. If you are just trying to sort arrays then my code already does that and you could use it. If you are trying to sort by reflection (which is what it looks like due to your call to ValueObject) then you will have to change my code. What does this ValueObject do? Is this a homework assignment? I gave you working code, and it doesn't look like you have gone to much effort to understand it.
 
jay lai
Ranch Hand
Posts: 180
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The ValueObject is just a class store all the field the represent the columns needed to be sort on.

I read your code througoutly and try to make modified according to my need, some of your class library are not there, therefore i need to remove it.


class ValueObject
{
String name;
String state;
public ValueObject (String name, String state)
{

}
}
 
jay lai
Ranch Hand
Posts: 180
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The ValueObject is just a class store all the field the represent the columns needed to be sort on.

I read your code througoutly and try to make modified according to my need, some of your class library are not there, therefore i need to remove it.

My purpose is to create an ArrayList and add all the value into this ValueObject then sort the object using some of your "existing" code algorimth..



Also this is not a homework, this is for work.
Thanks again.

 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic