Re: A custom way to sort EOs by key ?
Re: A custom way to sort EOs by key ?
- Subject: Re: A custom way to sort EOs by key ?
- From: Chuck Hill <email@hidden>
- Date: Fri, 11 May 2007 12:52:59 -0700
On May 11, 2007, at 12:55 AM, Fabrice Pipart wrote:
On May 10, 2007, at 11:19 PM, Matthew W. Taylor wrote:
It sounds like what you want to do is perform "Natural" sort
ordering to the results.
sortedArray = unsortedArray.sortedArrayUsingComparator(new
EOCustomObjectNaturalOrderComparator("nameOfKeyToSortBy"));
// EOCustomObjectNaturalOrderComparator.java
That's absolutely what I was looking for :-)
I had just missed the NSArray.sortedArrayUsingComparator method :(
And it's far more obvious how to subclass a NSComparator than a
NSSelector ;-)
The only drawback compared to
EOSortOrdering.sortedArrayUsingKeyOrderArray is that I cannot do
ordering for several keys : ORDER BY name, otherKey ...
Absolutely you can sort by multiple keys! Here is how to do it:
/**
* NSComparator to sort on Zone, Production Status, and Product
Type. This relies on the Any status/type
* having a value of 0 so that more general associations sort
later in the list. The more specific ones
* earlier in the list have a higher priority.
*/
public static class TypeStatusComparator extends NSComparator
{
public int compare(Object object1, Object object2) throws
NSComparator.ComparisonException
{
int result =
NSComparator.AscendingNumberComparator.compare(((ColorSwatchZone)
object1).zone().zoneID(),
((ColorSwatchZone)object2).zone().zoneID());
if (result == NSComparator.OrderedSame)
{
result =
NSComparator.DescendingNumberComparator.compare(((ColorSwatchZone)
object1).productionStatus(),
((ColorSwatchZone)object2).productionStatus());
}
if (result == NSComparator.OrderedSame)
{
result =
NSComparator.DescendingNumberComparator.compare(((ColorSwatchZone)
object1).productType(),
((ColorSwatchZone)object2).productType());
}
return result;
}
}
Chuck
--
Practical WebObjects - for developers who want to increase their
overall knowledge of WebObjects or who are trying to solve specific
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