Sorting an array

I cannot see my way to sorting an array of points using a simple float comparison.

Here’s the essence of what I’m seeking to do:

import java.util.Collections;

void setup(){
  ArrayList<Point> points = new ArrayList();
  PVector p0 = new PVector(100, -100);
  PVector p1 = new PVector(-130, 65);
  PVector p2 = new PVector(135, 155);
  points.add( new Point(p0, p0.heading()) );
  points.add( new Point(p1, p1.heading()) );
  points.add( new Point(p2, p2.heading()) );
  
  Collections.sort(points, (a,b) -> a.heading < b.heading ? -1 : 1);
}

class Point  {
  PVector loc;
  float heading;
  
  Point(PVector _loc, float _heading){
    loc = _loc;
    heading = _heading;
  }
}

Apparently I’m only allowed to use lambda expressions at source level 1.8 or above, whatever that means, so that doesn’t work.

I’ve found many examples that tell me what I should be doing, but none of them seem to be working. For example this tells me this should work:

class Point implements Comparator<Point> {
  PVector loc;
  float heading;
  
  Point(PVector _loc, float _heading){
    loc = _loc;
    heading = _heading;
  }
  
   // Overriding the compare method to sort the heading 
   public float compare(Point p0, Point p1) {
      return p0.heading - p1.heading;
   }
}

but apparently The return type is incompatible with Comparator<sketch_xxx.Point>.compare(sketch_xxx.Point,sketch_xxx.Point)

Other websites tell me to do other things, such as overriding Comparable<Point> compareTo(), which I haven’t gotten to work either. (I’ll save everyone the trouble of the profusion of links I visited, unless asked for them.)

I would be grateful if someone can please let me know a way that works in processing.

1 Like

Can you post the rest of your sketch for the second example?

From what you’ve posted so far, the one thing that stands out to me is it feels a little weird for the Point class to also be a Comparator for Point- I would expect Point to be a Comparable, and a Comparator to be a separact class. But I don’t see anything obviously wrong about what you have, so it probably depends on how you’re using it.

2 Likes
// https://Discourse.processing.org/t/sorting-an-array/20992/3
// GoToLoop (2020/May/17)

import java.util.Collections;
import java.util.List;

final PVector[] vecs = {
  new PVector(100, -100), 
  new PVector(-130, 65), 
  new PVector(135, 155)
};

final List<VecHead> points = new ArrayList<VecHead>(vecs.length);

void setup() {
  for (final PVector v : vecs)  points.add(new VecHead(v));
  println(points);

  Collections.sort(points);
  println(points);

  exit();
}

static class VecHead extends PVector implements Comparable<VecHead> {
  float heading;

  VecHead(final PVector vec) {
    heading = set(vec).heading();
  }

  VecHead reheading() {
    heading = heading();
    return this;
  }

  @Override int compareTo(final VecHead v) { // ascending
    return (int) Math.signum(heading - v.heading);
  }

  @Override String toString() {
    return super.toString() + " (heading: " + heading + ')';
  }
}
// https://Discourse.processing.org/t/sorting-an-array/20992/4
// GoToLoop (2020/May/18)

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

final PVector[] vecs = {
  new PVector(100, -100), 
  new PVector(-130, 65), 
  new PVector(135, 155)
};

static final Comparator<PVector> HEADING = new Comparator<PVector>() {
  @Override final int compare(final PVector a, final PVector b) {
    return (int) Math.signum(a.heading() - b.heading());
  }
};

static final Comparator<PVector> HEADING_REVERSED = HEADING.reversed();

void setup() {
  println(vecs);
  printVecHeadings();
  println();

  Arrays.sort(vecs, HEADING);  // ascending
  println(vecs);
  printVecHeadings();
  println();

  Arrays.sort(vecs, HEADING_REVERSED);  // descending
  println(vecs);
  printVecHeadings();

  exit();
}

void printVecHeadings() {
  for (final PVector v : vecs)  print(v.heading() + "              ");
}
3 Likes

Thank you as always GoToLoop!

The original code example now works with a new understanding of comparators gleaned from your examples (and some doc research).

import java.util.Comparator;

void setup(){
  ArrayList<PVector> points = new ArrayList();
  PVector p0 = new PVector(100, -100);
  PVector p1 = new PVector(-130, 65);
  PVector p2 = new PVector(135, 155);
  points.add( p0 );
  points.add( p1 );
  points.add( p2 );
  
  println(points);
  points.sort(pvHeading);
  println(points);
}

public static Comparator<PVector> pvHeading = new Comparator<PVector>() {         
  @Override         
  public int compare(PVector a, PVector b) {             
    return (int) Math.signum(a.heading() - b.heading());
  }     
};
2 Likes

A post was split to a new topic: Engine that uses js Processing