Java 里的Comparator接口里的compare方法怎么确定升降序的?
怎么确定升降序的?根据返回值还是什么?比如接口里的方法publicintcompar(Objectobject1,Objectobject2){Stringa=(Stri...
怎么确定升降序的?根据返回值还是什么?
比如接口里的方法 public int compar(Object object1, Object object2){
String a = (String) object1;
String b = (String) object2;
return a>b;
}
此时是升序,还是降序?怎么确定的? 展开
比如接口里的方法 public int compar(Object object1, Object object2){
String a = (String) object1;
String b = (String) object2;
return a>b;
}
此时是升序,还是降序?怎么确定的? 展开
展开全部
不是要用return a.compareTo(b);进行比较的吗?compareTo()方法是在String类下面的
Compares two strings lexicographically. The comparison is based on the Unicode
value of each character in the strings. The character sequence represented by
this String object is compared lexicographically to the character
sequence represented by the argument string. The result is a negative integer if
this String object lexicographically precedes the argument string.
The result is a positive integer if this String object
lexicographically follows the argument string. The result is zero if the strings
are equal; compareTo returns 0 exactly when the equals(Object) method would return true.
Compares two strings lexicographically. The comparison is based on the Unicode
value of each character in the strings. The character sequence represented by
this String object is compared lexicographically to the character
sequence represented by the argument string. The result is a negative integer if
this String object lexicographically precedes the argument string.
The result is a positive integer if this String object
lexicographically follows the argument string. The result is zero if the strings
are equal; compareTo returns 0 exactly when the equals(Object) method would return true.
本回答被网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
sort会自动调用哪个compare方法,如果他没有实现,则无法排序,也就是顺序是混乱的。
另一方面,你指定了comp方法,则系统在每次需要比较时,都把2个对象传到里面,注意compareabel的参数是 (Object o1,Object o2)
也就是默认是Object类型的,里面怎么比较和类型判断是程序员的事情
当然你也可以启用泛型,指定compare的对象类型。
另一方面,你指定了comp方法,则系统在每次需要比较时,都把2个对象传到里面,注意compareabel的参数是 (Object o1,Object o2)
也就是默认是Object类型的,里面怎么比较和类型判断是程序员的事情
当然你也可以启用泛型,指定compare的对象类型。
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
自定义排序方法
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询