| Author |
Please help with sorting multiple columns
|
jay lai
Ranch Hand
Joined: Apr 04, 2002
Posts: 180
|
|
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
Joined: Aug 20, 2001
Posts: 1815
|
|
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.
|
Piscis Babelis est parvus, flavus, et hiridicus, et est probabiliter insolitissima raritas in toto mundo.
|
 |
jay lai
Ranch Hand
Joined: Apr 04, 2002
Posts: 180
|
|
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
Joined: Apr 04, 2002
Posts: 180
|
|
Can anyone please help, this is urgent... Greatly appreciated.
|
 |
Burkhard Hassel
Ranch Hand
Joined: Aug 25, 2006
Posts: 1274
|
|
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.
|
all events occur in real time
|
 |
Stan James
(instanceof Sidekick)
Ranch Hand
Joined: Jan 29, 2003
Posts: 8791
|
|
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.
|
A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi
|
 |
jay lai
Ranch Hand
Joined: Apr 04, 2002
Posts: 180
|
|
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
Joined: Jun 26, 2002
Posts: 852
|
|
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 ]
|
http://www.jamonapi.com/ - a fast, free open source performance tuning api.
JavaRanch Performance FAQ
|
 |
jay lai
Ranch Hand
Joined: Apr 04, 2002
Posts: 180
|
|
Thanks Steve I am going to look into it, Greatly appreciated for your sharing and helping. Regards
|
 |
jay lai
Ranch Hand
Joined: Apr 04, 2002
Posts: 180
|
|
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
Joined: Jun 26, 2002
Posts: 852
|
|
|
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
Joined: Apr 04, 2002
Posts: 180
|
|
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
Joined: Apr 04, 2002
Posts: 180
|
|
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.
|
 |
 |
|
|
subject: Please help with sorting multiple columns
|
|
|