• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: A custom way to sort EOs by key ?
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: A custom way to sort EOs by key ?


  • Subject: Re: A custom way to sort EOs by key ?
  • From: Mike Schrag <email@hidden>
  • Date: Fri, 11 May 2007 16:06:17 -0400

or (untested ... syntactically correct is all i can vouch for here):

public class CompositeComparator extends NSComparator {
  private NSComparator[] _comparators;

  public CompositeComparator(NSComparator... comparators) {
    _comparators = comparators;
  }

@Override
public int compare(Object object1, Object object2) throws ComparisonException {
int result = NSComparator.OrderedSame;
for (NSComparator comparator : _comparators) {
result = comparator.compare(object1, object2);
if (result != NSComparator.OrderedSame) {
break;
}
}
return result;
}
}


On May 11, 2007, at 3:52 PM, Chuck Hill wrote:


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:
40mdimension.com


This email sent to email@hidden


_______________________________________________
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


References: 
 >Re: A custom way to sort EOs by key ? (From: Robert Walker <email@hidden>)
 >Re: A custom way to sort EOs by key ? (From: "Matthew W. Taylor" <email@hidden>)
 >Re: A custom way to sort EOs by key ? (From: Fabrice Pipart <email@hidden>)
 >Re: A custom way to sort EOs by key ? (From: Chuck Hill <email@hidden>)

  • Prev by Date: Re: A custom way to sort EOs by key ?
  • Next by Date: Frontbase boolean and EO...
  • Previous by thread: Re: A custom way to sort EOs by key ?
  • Next by thread: Re: A custom way to sort EOs by key ?
  • Index(es):
    • Date
    • Thread