On Jul 26, 2004, at 3:21 PM, McBrayer, Roy wrote:
> Is there a way to order a query of a varchar field that actually
> represents
> numbers in a similar fashion to using the toNumber function in SQL?
>
> Right now, as expected, 16 is ordered before 2 since I am dealing with
> Varchars.
>
> Thanks,
> Roy
As Cayenne doesn't support explicit type conversion functions in
expressions (including orderings), I guess the best bet is in-memory
sorting of the list using a custom ordering subclass that does the
conversion. I wonder if anyone has better ideas?
E.g.:
Ordering ordering = new MyOrdering("someProperty", true);
ordering.orderList(myObjects);
class MyOrdering extends org.objectstyle.cayenne.query.Ordering {
// constructors, etc...
// override compare
public int compare(Object o1, Object o2) {
Object value1 = sortSpec.evaluate(o1);
Object value2 = sortSpec.evaluate(o2);
// check for nulls
if (value1 == null) {
return (value2 == null) ? 0 : -1;
}
else if (value2 == null) {
return 1;
}
// convert to numbers
Number n1 = new Integer(value1.toString());
Number n2 = new Integer(value2.toString());
int compareResult = n1.compareTo(n2);
return (ascending) ? compareResult : -compareResult;
}
}
Andrus
This archive was generated by hypermail 2.0.0 : Mon Jul 26 2004 - 16:27:48 EDT