Android java使用Comparable自定义排序

发布于 2024-11-16 15:16:06 字数 1806 浏览 1 评论 0原文

  @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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

笑叹一世浮沉 2024-11-23 15:16:06

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

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


}

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:

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


}
半城柳色半声笛 2024-11-23 15:16:06

你的 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 return BEFORE.

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).

滥情空心 2024-11-23 15:16:06

我现在的代码:

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

谢谢,桑杰!

my code now :

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

Thanks, Sanjay!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文