Re: NSComparator - sorting objects in a NSMutableArray

Mike Schrag <[email protected]>
Newsgroups gmane.comp.web.webobjects.devel
Message-ID <[email protected]>
EOSortOrdering.sortArrayUsingKeyOrderArray(yourArray, new NSArray 
(EOSortOrdering.sortOrderingWithKey("keypath.to.double.attribute",  
EOSortOrdering.CompareAscending)));

On Jun 22, 2006, at 3:13 AM, wojingo wrote:

> Hi Matt,
>
> Matt Kime wrote:
>> I have an NSMutableArray populated with objects. I want do sort them
>> based on a double type property. sortUsingComparator seems to be the
>> way to go. this means i need to implement a "compare" function in the
>> object.
>> this is the first time i'm tackling this technique and i'm finding
>> the javadocs too terse to understand. does anyone know where i can
>> find an example of this?
>
> This might help.
> http://java.sun.com/j2se/1.4.2/docs/api/java/lang/Comparable.html
>
> I think the following will work.. However I just wrote it and  
> havent tried it so it probably will have something(s) wrong.  :)
>
> public int compare(Object o1, Object o2)
>     throws NSComparator.ComparisonException {
>
>   if (! (o1 instanceof o2) ) {
>       throw new NSComparator.ComparisonException(
>                 "null reference or wrong class");
>   }
>
>   double d1 = ((MyObject)o1).myDouble;
>   double d2 = ((MyObject)o2).myDouble;
>
>   if (d1 == d2) return NSComparator.OrderedSame;
>   if (d1 < d2 ) return NSComparator.OrderAscending;
>   return NSComparator.OrderDescending;
> }
>
>
> cheers,
>  - shaun
> _______________________________________________
> WebObjects-dev mailing list
> [email protected]
> http://www.omnigroup.com/mailman/listinfo/webobjects-dev
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.