使用Comparable进行Android java自定义排序

时间:2011-06-19 12:51:51

标签: java android sorting

  @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());
      }

我有一个String用户列表,带有昵称。我已禁止用户,普通用户和朋友用户。

我想将我的朋友用户排在列表顶部,将被禁用的用户排在列表底部,我想将它们显示为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!

3 个答案:

答案 0 :(得分:5)

您的代码看起来有点复杂。最简单的方法是从最高优先级字段开始,然后向下移动到其他字段。示例代码为:

public class Member implements Comparable<Member> {

    static enum Status {
        NORMAL(1), FRIEND(2), BANNED(3);

        private final int order;

        Status(int order) {
            this.order = order;
        }

        public int getOrder() {
            return this.order;
        }

    };

    private final String name;

    private final Status status;

    public Member(final String name, final Status status) {
        this.name = name;
        this.status = status;
    }

    @Override
    public int compareTo(Member o) {
        if (this.status.equals(o.status)) {
            return this.name.compareTo(o.name);
        } else {
            return this.status.compareTo(o.status);
        }
    }

    @Override
    public String toString() {
        return "Member [name=" + name + ", status=" + status + "]";
    }

    public static void main(String[] args) throws Throwable {
        Member[] members = {
                        new Member("abrt", Status.FRIEND),
                        new Member("dfgh", Status.FRIEND),
                        new Member("abdf", Status.NORMAL),
                        new Member("wert", Status.NORMAL),
                        new Member("andgh", Status.BANNED),
                        new Member("qwer", Status.BANNED)
        };
        List<Member> lst = Arrays.asList(members);
        Collections.sort(lst);
        System.out.println(lst);
    }


}

答案 1 :(得分:1)

您的ABC排序逻辑不起作用。例如,如果您传入两个USER_TYPE_FRIEND个对象,无论其各自的顺序如何,compareTo将始终返回BEFORE

您需要首先比较usertype。

如果它们相同,您可以返回row.compareTo(...)表达式。

如果没有,你需要在之前/之后返回,只取决于你的逻辑中这些类型“比较”的方式(即朋友&lt; normal&lt; banned)。

答案 2 :(得分:0)

我现在的代码:

  public enum user_type {USER_TYPE_FRIEND(1), USER_TYPE_NORMAL(2), USER_TYPE_BANNED(3);
      private final int order;
      user_type(int order) {
          this.order = order;
      }
      public int getOrder() {
          return this.order;
      }
  }

...

  @Override
  public int compareTo(final myRow another) {

    if (sorttype==sort_type.SORT_ABC) {
        if (this.getUserType().equals(another.getUserType())) {
            return this.getRow().toLowerCase().compareTo(another.getRow().toLowerCase());
        } else {
            return this.getUserType().compareTo(another.getUserType());
        }
    } 
    else {
        //LOGINTIME
        return this.getUserType().compareTo(another.getUserType());
    }
  }

谢谢,Sanjay!