Re: Null values at end of sorted list
Re: Null values at end of sorted list
- Subject: Re: Null values at end of sorted list
- From: Lachlan Deck <email@hidden>
- Date: Sat, 2 Feb 2008 07:21:19 +1100
Hi Aaron,
On 01/02/2008, at 5:34 AM, email@hidden wrote:
<...>
We've tried our hand at a solution which is listed at the bottom of
this message. It works so long as no commits have been made to the
database! After a commit, from any page in the app, sorting no
longer works as expected and often results in a K2Sort error. It's
baffling.
Our class extends the default sorting logic of
EOSortOrdering.ComparisonSupport (the class that does sorting
globally for WO). We really only want to override "handleNulls()"
but because it is both private and static, we need to redo a few
other methods as well. Functionally, our class is identical but
choose to place null values at the end of the list for an ascending
sort.
Here is the line we add to the Appliction's constructor to get the
ball rolling globally:
==========================
EOSortOrdering.ComparisonSupport.setSupportForClass(new
CXNullsAtEndComparisonSupport(), Object.class);
Seeing as you've peeked at _handleNulls etc you, on closer
inspection, should notice that this is not what you want.
for ( Enumeration mn = EOModelGroup.defaultGroup().models
().objectEnumerator(); mn.hasMoreElements(); )
{
EOModel model = ( EOModel )mn.nextElement();
for ( Enumeration en = model.entities().objectEnumerator();
en.hasMoreElements(); )
{
EOEntity entity = ( EOEntity )en.nextElement();
Class entityClass = Class.forName( entity.className() );
EOSortOrdering.ComparisonSupport.setSupportForClass( new
CXNullsAtEndComparisonSupport(), entityClass );
}
}
Here is the source code to the custom ComparisonSupport:
==========================
public class CXNullsAtEndComparisonSupport extends
EOSortOrdering.ComparisonSupport {
public static final Logger _logger = Logger.getLogger
(CXNullsAtEndComparisonSupport.class);
public CXNullsAtEndComparisonSupport() {}
protected static final int kNeitherNull = -42;
protected static int handleNulls(Object left, Object right)
{
if(left == null || left == NSKeyValueCoding.NullValue)
return right != null && right !=
NSKeyValueCoding.NullValue ? NSComparator.OrderedDescending :
NSComparator.OrderedSame;
return right != null && right !=
NSKeyValueCoding.NullValue ? kNeitherNull :
NSComparator.OrderedAscending;
}
private static final NSTimestampFormatter
defaultStringTimestampFormatter = new NSTimestampFormatter("%Y-%m-%
d %H:%M:%S %Z");
// NSTimestampFormatter is deprecated
private static final Format defaultStringTimestampFormatter = new
SimpleDateFormat("yyyy-MM-dd hh:mm:ss z");
protected static NSTimestamp coerceToTimestampClass(Object
value)
{
if(value instanceof NSTimestamp)
return (NSTimestamp)value;
if(value instanceof Date)
return new NSTimestamp(((Date)value).getTime
());
if(value instanceof Number)
return new NSTimestamp(((Number)
value).longValue());
try {
Date date = defaultStringTimestampFormatter.parse(value.toString());
return coerceToTimestampClass(date);
} catch (Exception e) {
//fixme: do something more.
_logger.info("coerceToTimestampClass
failed. Message = " + e.getMessage());
return null;
}
}
<...>
with regards,
--
Lachlan Deck
_______________________________________________
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