Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.java.programmer > #9742

Re: Did the sort do anything?

Path csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!weretis.net!feeder4.news.weretis.net!eternal-september.org!feeder.eternal-september.org!.POSTED!not-for-mail
From markspace <-@.>
Newsgroups comp.lang.java.programmer
Subject Re: Did the sort do anything?
Date Mon, 07 Nov 2011 07:48:16 -0800
Organization A noiseless patient Spider
Lines 74
Message-ID <j98uk2$ud7$1@dont-email.me> (permalink)
References <4d8fb7l8qb1g820cphr4fh447a9uitlddj@4ax.com>
Mime-Version 1.0
Content-Type text/plain; charset=ISO-8859-1; format=flowed
Content-Transfer-Encoding 7bit
Injection-Date Mon, 7 Nov 2011 15:48:18 +0000 (UTC)
Injection-Info mx04.eternal-september.org; posting-host="XjIWM99mD7Ijfdu600oVPA"; logging-data="31143"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1+Tsu7sZaTOUKXfMnX9Y1/4FXfjY//CW38="
User-Agent Mozilla/5.0 (Windows NT 6.1; WOW64; rv:7.0.1) Gecko/20110929 Thunderbird/7.0.1
In-Reply-To <4d8fb7l8qb1g820cphr4fh447a9uitlddj@4ax.com>
Cancel-Lock sha1:MfsFZA8HtyC5nS+zf0TUPwmE42s=
Xref x330-a1.tempe.blueboxinc.net comp.lang.java.programmer:9742

Show key headers only | View raw


On 11/7/2011 1:27 AM, Roedy Green wrote:
> What is the easiest way to determine if a sort actually changed the
> order?


The best and most correct would be to scan the new array for changes. 
If you're willing to relax your definition of "change" (and maybe 
"best"), you could just test if any element was out of order while the 
list was sorted.  If so, you can assume the list was changed, and if 
not, you might assume it was not.

Here's my attempt:


package quicktest;

import java.util.Arrays;
import java.util.Comparator;


/**
  *
  * @author Brenden
  */
public class LatchOutOfOrderComparator<T> implements Comparator<T> {

    private Comparator delegate;
    private boolean wasOutOfOrder;

    public LatchOutOfOrderComparator( Comparator delegate ) {
       this.delegate = delegate;
    }

    public boolean getWasOutOfOrder() {
       return wasOutOfOrder;
    }

    @Override
    public int compare(T o1, T o2) {
       int compare = delegate.compare( o1, o2 );
       if( compare < 0 ) wasOutOfOrder = true;
       return compare;
    }

}


class LatchOutOfOrderComparableTest {

    public static void main(String[] args) {
       String[] reversed = {"c", "b", "a" };
       System.out.println( Arrays.toString( reversed ) +
       " out of order:" );
       LatchOutOfOrderComparator l1 = new LatchOutOfOrderComparator(
               new StringComapartor() );
       Arrays.sort( reversed, l1 );
       System.out.println( l1.getWasOutOfOrder() );

       String[] ordered = {"a", "b", "c" };
       System.out.println( Arrays.toString( ordered ) +
       " out of order:" );
       LatchOutOfOrderComparator l2 = new LatchOutOfOrderComparator(
               new StringComapartor() );
       Arrays.sort( ordered, l2 );
       System.out.println( l2.getWasOutOfOrder() );
    }

    private static class StringComapartor implements Comparator<String> {

       public int compare(String o1, String o2) {
          return o1.compareTo(o2);
       }
    }
}

Back to comp.lang.java.programmer | Previous | NextPrevious in thread | Next in thread | Find similar


Thread

Did the sort do anything? Roedy Green <see_website@mindprod.com.invalid> - 2011-11-07 01:27 -0800
  Re: Did the sort do anything? Roedy Green <see_website@mindprod.com.invalid> - 2011-11-07 02:20 -0800
    Re: Did the sort do anything? Andreas Leitgeb <avl@gamma.logic.tuwien.ac.at> - 2011-11-07 11:21 +0000
  Re: Did the sort do anything? Andreas Leitgeb <avl@gamma.logic.tuwien.ac.at> - 2011-11-07 10:49 +0000
    Re: Did the sort do anything? Roedy Green <see_website@mindprod.com.invalid> - 2011-11-07 02:58 -0800
  Re: Did the sort do anything? Eric Sosman <esosman@ieee-dot-org.invalid> - 2011-11-07 07:11 -0500
    Re: Did the sort do anything? Joshua Cranmer <Pidgeot18@verizon.invalid> - 2011-11-07 08:30 -0600
      Re: Did the sort do anything? Eric Sosman <esosman@ieee-dot-org.invalid> - 2011-11-07 21:03 -0500
  Re: Did the sort do anything? markspace <-@.> - 2011-11-07 07:48 -0800
  Re: Did the sort do anything? dagon@dagon.net (Dagon) - 2011-11-07 12:50 -0800
    Re: Did the sort do anything? Cindy <c.thurston@frell.okb.uwa.edu> - 2011-11-07 22:02 -0500
      Re: Did the sort do anything? Cindy <c.thurston@frell.okb.uwa.edu> - 2011-11-07 22:22 -0500
  Re: Did the sort do anything? Arne Vajhøj <arne@vajhoej.dk> - 2011-11-07 17:42 -0500

csiph-web