我看java核心技术时有些地方想不通
就是
Array要实现对employee对象数组排序
就是用sort排序
staff[0]=new employee("harry hacker",35000);
staff[1]=new employee("car1 cracker",75000);
staff[2]=new employee("tony tester",38000);Arrays.sort(staff);结果应该是什么呢?是按字母排序还是按后面的工资排序?为什么可以实现这种排序呢?
实现这个有什么用呢?书上说得不太清楚请大家帮我解释一下,实现下面这个方法就能进行排序?
class employee implements Comparable
{
  ..............
  public int comparaTO(Object otherObject)
 {
  employee other=(employee)otherObject;
  if(salary<other.salary) return -1;
  if (salary>other.salary) return 11;
 return 0;
 }
 ................
}

解决方案 »

  1.   

    Comparable这个接口是怎么定义的啊
      

  2.   

    sort实际是调用comparaTO进行比较,当然要实现Comparable接口了,很明显是通过工资排序。
      

  3.   

    java.lang.Comparable
    不用自已定义。
      

  4.   

    sort
    public static void sort(Object[] a)Sorts the specified array of objects into ascending order, according to the natural ordering of its elements. All elements in the array must implement the Comparable interface. Furthermore, all elements in the array must be mutually comparable (that is, e1.compareTo(e2) must not throw a ClassCastException for any elements e1 and e2 in the array).
    This sort is guaranteed to be stable: equal elements will not be reordered as a result of the sort.The sorting algorithm is a modified mergesort (in which the merge is omitted if the highest element in the low sublist is less than the lowest element in the high sublist). This algorithm offers guaranteed n*log(n) performance. 那要看你的compareTo怎么定义的了! 想用那个排都可以!要多翻一下jdk文档! 你所问的文档里面都有!
      

  5.   

    Arrays.sort()这是个函数中调用comparaTO函数判断大小。然后排序。