package de.lmu.ifi.dbs.elki.result.outlier;

import java.util.Collections;

import de.lmu.ifi.dbs.elki.database.ids.ArrayModifiableDBIDs;
import de.lmu.ifi.dbs.elki.database.ids.DBID;
import de.lmu.ifi.dbs.elki.database.ids.DBIDUtil;
import de.lmu.ifi.dbs.elki.database.ids.DBIDs;
import de.lmu.ifi.dbs.elki.result.AnnotationResult;
import de.lmu.ifi.dbs.elki.result.OrderingResult;
import de.lmu.ifi.dbs.elki.utilities.datastructures.IterableIterator;
import de.lmu.ifi.dbs.elki.utilities.datastructures.IterableIteratorAdapter;

/**
 * Ordering obtained from an outlier score.
 * 
 * @author Erich Schubert
 */
public class OutlierOrderingResult implements OrderingResult {
  /**
   * Outlier scores.
   */
  private AnnotationResult<Double> scores;

  /**
   * Factor for ascending (+1) and descending (-1) ordering.
   */
  boolean ascending;
  
  /**
   * Constructor for outlier orderings
   * 
   * @param scores outlier score result
   * @param ascending Ascending when {@code true}, descending otherwise
   */
  public OutlierOrderingResult(AnnotationResult<Double> scores, boolean ascending) {
    super();
    this.scores = scores;
    this.ascending = ascending;
  }
  
  /**
   * Ascending constructor.
   * 
   * @param scores
   */
  public OutlierOrderingResult(AnnotationResult<Double> scores) {
    this(scores, true);
  }

  @Override
  public String getLongName() {
    return scores.getLongName()+" Order";
  }

  @Override
  public String getShortName() {
    return scores.getShortName()+"_order";
  }

  @Override
  public IterableIterator<DBID> iter(DBIDs ids) {
    ArrayModifiableDBIDs sorted = DBIDUtil.newArray(ids);
    if (ascending) {
      Collections.sort(sorted);
    } else {
      Collections.sort(sorted, Collections.reverseOrder());
    }
    return new IterableIteratorAdapter<DBID>(sorted);
  }
}

@@ -67,16 +67,45 @@
 ections;
+%0Aimport java.util.Comparator;
 %0A%0Aimport
@@ -739,13 +739,15 @@
   pr
-ivate
+otected
  Ann
@@ -852,23 +852,29 @@
    */%0A  
-boolean
+protected int
  ascendi
@@ -871,24 +871,29 @@
 nt ascending
+ = +1
 ;%0A  %0A  /**%0A 
@@ -1208,16 +1208,26 @@
 scending
+ ? +1 : -1
 ;%0A  %7D%0A  
@@ -1577,257 +1577,611 @@
 %0A%0A  
-@Override%0A  public IterableIterator%3CDBID%3E iter(DBIDs ids) %7B%0A    ArrayModifiableDBIDs sorted = DBIDUtil.newArray(ids);%0A    if (ascending) %7B%0A      Collections.sort(sorted);%0A    %7D else %7B%0A      Collections.sort(sorted, Collections.reverseOrder());%0A    %7D
+/**%0A   * Internal comparator, accessing the map to sort objects%0A   * %0A   * @author Erich Schubert%0A   */%0A  protected final class ImpliedComparator implements Comparator%3CDBID%3E %7B%0A    @Override%0A    public int compare(DBID id1, DBID id2) %7B%0A      Double k1 = scores.getValueFor(id1);%0A      Double k2 = scores.getValueFor(id2);%0A      assert (k1 != null);%0A      assert (k2 != null);%0A      return ascending * k2.compareTo(k1);%0A    %7D%0A  %7D%0A%0A  @Override%0A  public IterableIterator%3CDBID%3E iter(DBIDs ids) %7B%0A    ArrayModifiableDBIDs sorted = DBIDUtil.newArray(ids);%0A    Collections.sort(sorted, new ImpliedComparator());
 %0A   

Fix ordering result.