我有一个以这种方式实现Comparator的类:
public class MyObject<T>
{
public static class MyObjectComp<T> implements Comparator<MyObject<T>>
{
private LinkedHashSet<Integer> attrList;
public MyObjectComp (int[] intList)
{
this.attrList = new LinkedHashSet<Integer>();
for (int idx: intList)
attrList.add(idx);
}
public MyObjectComp (LinkedHashSet<Integer> attrList)
{
this.attrList = attrList;
}
public int compare(MyObject<T> pf1, MyObject<T> pf2)
{
for (Integer idx: attrList)
{
double pf1Norm = pf1.atribute.get(idx).myList.size();
double pf2Norm = pf2.atribute.get(idx).myList.size();
if (pf1Norm > pf2Norm)
return 1;
else if (pf1Norm < pf2Norm)
return -1;
}
return (pf1.key > pf2.key) ? 1 : ((pf1.key < pf2.key) ? -1 : 0);
}
}
}
在scala编写的代码的另一部分中,我使用此MyObject创建了一个RDD。现在我需要使用MyObject的内部比较器类来排序这个RDD的元素。我怎么能使用像myRDD. sort()?
答案 0 :(得分:1)
类似的东西(注意:没试过这段代码)
implicit val moo = new MyObjectComp[ T ]( /* ... */ ).asScala
myRDD.sortBy( t => t )