Android java使用Comparable自定义排序
@Override
public int compareTo(final myRow another) {
final int BEFORE =-1;
final int EQUAL = 0;
final int AFTER = 1;
if (this==another) return EQUAL;
if (sorttype==sort_type.SORT_ABC) {
int rv =0;
int sorted =row.toLowerCase().compareTo(another.getRow().toLowerCase());
if (this.getUserType()==user_type.USER_TYPE_BANNED) rv=AFTER;
if (this.getUserType()==user_type.USER_TYPE_NORMAL) rv=sorted;
if (this.getUserType()==user_type.USER_TYPE_FRIEND) rv=BEFORE;
Log.e("sorted", row+" "+this.getUserType()+" - "+rv+" ");
return rv;
} else if (sorttype==sort_type.SORT_LOGINTIME) {
if (this.getUserType()==user_type.USER_TYPE_BANNED) {
return AFTER;
}
if (this.getUserType()==user_type.USER_TYPE_NORMAL) return EQUAL;
if (this.getUserType()==user_type.USER_TYPE_FRIEND) {
//int sorted =row.toLowerCase().compareTo(another.getRow().toLowerCase());
return BEFORE;
}
//return 0;
}
return 0;
//return row.toLowerCase().compareTo(another.getRow().toLowerCase());
}
我有一个带有昵称的字符串用户列表。我已经封禁了用户、普通用户、好友用户。
我想将我的朋友用户排序到列表顶部,将被禁止的用户排序到列表底部,并且我想以 ASC 风格显示它们。
我怎样才能做到这一点?
所以结构是:
Abc (friend)
abrt (friend)
dfgh (friend)
abdfg (normal user)
bnmm (normal user)
wert (normal user)
Andgh (banned user)
Dfghhj (banned user)
Qwer (banned user)
我明白了:
06-19 14:43:46.586: ERROR/AndroidRuntime(23434): java.lang.IllegalArgumentException: Comparison method violates its general contract!
@Override
public int compareTo(final myRow another) {
final int BEFORE =-1;
final int EQUAL = 0;
final int AFTER = 1;
if (this==another) return EQUAL;
if (sorttype==sort_type.SORT_ABC) {
int rv =0;
int sorted =row.toLowerCase().compareTo(another.getRow().toLowerCase());
if (this.getUserType()==user_type.USER_TYPE_BANNED) rv=AFTER;
if (this.getUserType()==user_type.USER_TYPE_NORMAL) rv=sorted;
if (this.getUserType()==user_type.USER_TYPE_FRIEND) rv=BEFORE;
Log.e("sorted", row+" "+this.getUserType()+" - "+rv+" ");
return rv;
} else if (sorttype==sort_type.SORT_LOGINTIME) {
if (this.getUserType()==user_type.USER_TYPE_BANNED) {
return AFTER;
}
if (this.getUserType()==user_type.USER_TYPE_NORMAL) return EQUAL;
if (this.getUserType()==user_type.USER_TYPE_FRIEND) {
//int sorted =row.toLowerCase().compareTo(another.getRow().toLowerCase());
return BEFORE;
}
//return 0;
}
return 0;
//return row.toLowerCase().compareTo(another.getRow().toLowerCase());
}
I have a String userlist, with nick names. I have banned users, normal users, and friend users.
I d like to sort my friend users to the top of the list, and the banned users to the bottom of the list, and I d like to showing them is ASC style.
How can i do that?
So the structure is:
Abc (friend)
abrt (friend)
dfgh (friend)
abdfg (normal user)
bnmm (normal user)
wert (normal user)
Andgh (banned user)
Dfghhj (banned user)
Qwer (banned user)
I get this:
06-19 14:43:46.586: ERROR/AndroidRuntime(23434): java.lang.IllegalArgumentException: Comparison method violates its general contract!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
你的代码看起来有点复杂。最简单的方法是从最高优先级字段开始,然后向下移动到其他字段。示例代码如下:
Your code looks a bit complex. The simplest way would be to start from the highest priority field and move down to other fields. An example code would be:
你的 ABC 排序逻辑不起作用。例如,如果您传入两个
USER_TYPE_FRIEND
对象,无论它们各自的顺序如何,compareTo
将始终返回BEFORE
。您需要首先比较用户类型来实现这一点。
如果它们相等,您可以返回
row.compareTo(...)
表达式。如果不是,您需要在之前/之后返回,仅取决于这些类型在您的逻辑中如何“比较”(即朋友<正常<禁止)。
Your ABC sort logic doesn't work. If you pass in two
USER_TYPE_FRIEND
objects for instance, whatever their respective order,compareTo
will always returnBEFORE
.You need to implement this by first comparing the usertype.
If they are equal, you can return your
row.compareTo(...)
expression.If not, you need to return before/after depending only on how those types "compare" in your logic (i.e. friend < normal < banned).
我现在的代码:
...
谢谢,桑杰!
my code now :
...
Thanks, Sanjay!