Arrays 处理2.0数组排序

Arrays 处理2.0数组排序,arrays,sorting,processing,Arrays,Sorting,Processing,我在尝试对对象数组排序时遇到此错误: PApplet类型中的方法排序(字节[])不适用于参数(sketch_124453.Word[]) 为了支持sort,我看到必须实现Comparable,因此下面是我的类代码 但是它不起作用,所以我想知道在Processing 2.0+中是否可以对对象数组进行排序?我所做的这种解决方案方法是否只针对Processing 1.0 class Word implements Comparable { String s; int n=0; Word(

我在尝试对对象数组排序时遇到此错误:

PApplet类型中的方法排序(字节[])不适用于参数(sketch_124453.Word[])

为了支持sort,我看到必须实现Comparable,因此下面是我的类代码

但是它不起作用,所以我想知道在Processing 2.0+中是否可以对对象数组进行排序?我所做的这种解决方案方法是否只针对Processing 1.0

class Word implements Comparable  {
  String s;
  int n=0;
  Word(String theWord) {
    s = theWord;
    n = 1;
  }
  //if we want to sort based on the n value of Word object:
  int compareTo(Object o)
  {
    Word other=(Word)o;
    if(other.n>n)  return -1;
    if(other.n==n) return 0;
    return 1;
  }

  int compareTo(Word o)
  {    
    if(o.n>n)  return -1;
    if(o.n ==n) return 0;
    return 1;
  }
}

我认为要使用Comparable,您需要使用Java的
数组。sort()
,而不是Processing的数组。另外,您不需要两个
compareTo()
函数。作为奖励,我制作了一个
toString
函数,以便
println()
能够正确处理对象

看看吧

import java.util.Arrays;

Word[] words = new Word[3];

void setup() {

  words[0] = new Word("three", 3);
  words[1] = new Word("two", 2);
  words[2] = new Word("one", 1);

  println(words);

  Arrays.sort(words);

  println("sorted:");
  println(words);
}



class Word implements Comparable {
  String s;
  int n;
  Word(String theWord, int _n) {
    s = theWord;
    n = _n ;
  }
  //if we want to sort based on the n value of Word object:
  int compareTo(Object o)
  {
    Word other=(Word)o;
    if (other.n > n)  return -1;
    if (other.n == n) return 0;
    return 1;
  }

  String toString() {
    return s + " - n = " + n;
  }
}
由于需要导入
java.util.array
,这在processingjs中不起作用