Re: Sorting an array of EOs using my own selector [solved]
Re: Sorting an array of EOs using my own selector [solved]
- Subject: Re: Sorting an array of EOs using my own selector [solved]
- From: Chuck Hill <email@hidden>
- Date: Thu, 10 Mar 2005 12:17:56 -0800
On Mar 10, 2005, at 1:57 AM, Hugi Thordarson wrote:
Ah, of course I need to write the comparator for each class. Man, I
was lacking coffeine yesterday.
I kind of figured that was the case. :-)
For future reference for all the hordes of people who need to sort in
Icelandic (or other less significant languages ;-), here's a generic
comparator class that will sort any objects that implement KVC based
on a keypath, correctly given the right java.util.Locale. It's not
elegant, it's not well tested, it only supports strings and I believe
I ripped the String comparison code from someone else on this list.
Caveat emptor.
As a stylistic issue and also to protect the code against future
implementation changes, I'll suggest that people use
NSComparator.OrderedAscending
NSComparator.OrderedDescending
NSComparator.OrderedSame
Instead of -1,0, and 1. It does make the code rather easier to read.
Chuck
// Example use: Sorting an array of EOs by the attribute "name" case
insensitively in ascending (Icelandic) order:
GenericComparator comparator = new GenericComparator(
java.text.Collator.getInstance( new Locale("is", "IS" ) ), "name",
true, true);
myArray = myArray.sortedArrayUsingComparator( comparator );
// Start code
import com.webobjects.foundation.*;
import java.util.*;
import java.text.*;
public class GenericComparator extends NSComparator {
private Collator _collator;
private String _keyPath;
private boolean _ascending;
private boolean _caseInsensitive;
public GenericComparator( Collator collator, String keyPath, boolean
ascending, boolean caseInsensitive ) {
_collator = collator;
_keyPath = keyPath;
_ascending = ascending;
_caseInsensitive = caseInsensitive;
}
public int compare(Object kvc1, Object kvc2) throws
ComparisonException {
if(kvc1 == null || kvc2 == null || !(kvc1 instanceof
NSKeyValueCodingAdditions) || !(kvc2 instanceof
NSKeyValueCodingAdditions))
throw new ComparisonException( "Objects must implement
NSKeyValueCodingAdditions to be sortable." );
String string1 =
(String)((NSKeyValueCodingAdditions)kvc1).valueForKeyPath( _keyPath );
String string2 =
(String)((NSKeyValueCodingAdditions)kvc2).valueForKeyPath( _keyPath );
if(string1 == null || string2 == null )
throw new ComparisonException( "Sorry, I'm too lazy to implement
sorting for null values this time around :-)." );
if(string1 == string2)
return 0;
int i = _collator.compare((String)string1, (String)string2);
if(_caseInsensitive)
i = _collator.compare(((String)string1).toUpperCase(),
((String)string2).toUpperCase());
else
i = _collator.compare((String)string1, (String)string2);
if(i < 0)
return _ascending ? -1 : 1;
if(i > 0)
return _ascending ? 1 : -1;
else
return 0;
}
// End code
Cheers,
Hugi
On 9.3.2005, at 19:08, Chuck Hill wrote:
Hi Hugi!
It's been a while. I'm not clear on how you want to do the sorting.
If you just have an array of EOs then you can use NSComparator, I do
so all the time. I usually make them public inner classes of my EO
and provide a static instance for ease of use. If you are trying to
sort in a fetch spec then take a look at
com.webobjects.eocontrol.EOSortOrdering.ComparisonSupport
Chuck
On Mar 9, 2005, at 5:51 AM, Hugi Thordarson wrote:
Hi all!
I feel somewhat stupid not being able to figure this out on my own,
but here goes:
I have an array of EOs that I need to sort in Icelandic alphabetical
order (I can't use the DB for sorting, since I don't have access to
it to create a collation).
One would think that this should be simple enough, and I've thought
of several hacks; all of which make me jump through hoops that I
don't tink I should have to.
I have my own NSComparator subclass for comparing Icelandic strings
and I often use that to sort NSArrays of Strings - can I use this
comparator to sort the EOs somehow?
Cheers,
Hugi
// Hugi Thordarson
// Development mgr.
// Vefsyn hf. - development, analysis, marketing
// http://www.vefsyn.com/
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Webobjects-dev mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
email@hidden
This email sent to email@hidden
--
Practical WebObjects - a book for intermediate WebObjects developers
who want to increase their overall knowledge of WebObjects, or those
who are trying to solve specific application development problems.
http://www.global-village.net/products/practical_webobjects
--
Practical WebObjects - a book for intermediate WebObjects developers
who want to increase their overall knowledge of WebObjects, or those
who are trying to solve specific application development problems.
http://www.global-village.net/products/practical_webobjects
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Webobjects-dev mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden