这是按标题、位置排序然后使用比较器排序的正确方法吗?

发布于 2024-09-25 14:08:20 字数 542 浏览 2 评论 0原文

考虑一下这个类。

public class DynamicField implements Comparable<DynamicField> {
    String title;
    int position;
    int order;

    @Override
    public int compareTo(DynamicField o) {
        if(position < o.position) 
            return -1;
        if(position > o.position)
            return 1;

        if(order < o.order)
            return -1;
        if(order > o.order)
            return 1;

        return title.compareTo(o.title);

    }
}

如果我想按标题、位置然后顺序排序,compareTo 方法是否正确?

Consider this class.

public class DynamicField implements Comparable<DynamicField> {
    String title;
    int position;
    int order;

    @Override
    public int compareTo(DynamicField o) {
        if(position < o.position) 
            return -1;
        if(position > o.position)
            return 1;

        if(order < o.order)
            return -1;
        if(order > o.order)
            return 1;

        return title.compareTo(o.title);

    }
}

Is the compareTo method correct if I want to sort by title, position and then order?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(3

傻比既视感 2024-10-02 14:08:20

不,您正在以错误的顺序进行比较。重新排列比较顺序将使其起作用:

@Override
public int compareTo(DynamicField o) {
    int c = title.compareTo(o.title);
    if (c != 0)
      return c;
    if(position < o.position) 
        return -1;
    if(position > o.position)
        return 1;
    if(order < o.order)
        return -1;
    if(order > o.order)
        return 1;
    return 0;
}

No, you're making comparisons in an incorrect order. Rearranging comparisons order would make it work:

@Override
public int compareTo(DynamicField o) {
    int c = title.compareTo(o.title);
    if (c != 0)
      return c;
    if(position < o.position) 
        return -1;
    if(position > o.position)
        return 1;
    if(order < o.order)
        return -1;
    if(order > o.order)
        return 1;
    return 0;
}
拥抱没勇气 2024-10-02 14:08:20

不,试试这个代码
已更新

  public class DynamicField implements Comparable<DynamicField> {
        String title;
        int position;
        int order;

        @Override
        public int compareTo(DynamicField o) {
            int result = title.compareTo(o.title);
            if(result != 0) {}             
            else if(position != o.position)
                result = position-o.position;
            else if(order != o.order)
                result = order- o.order;

            return result;

        }
    }

No,Try this code
Updated

  public class DynamicField implements Comparable<DynamicField> {
        String title;
        int position;
        int order;

        @Override
        public int compareTo(DynamicField o) {
            int result = title.compareTo(o.title);
            if(result != 0) {}             
            else if(position != o.position)
                result = position-o.position;
            else if(order != o.order)
                result = order- o.order;

            return result;

        }
    }
人心善变 2024-10-02 14:08:20

这实际上与@org.life.java的答案相同。不过,你可能会发现这个更可口。

@Override
public int compareTo() {
    int result = title.compareTo(o.title);
    if (result == 0)
        result = position - o.position;
    if (result == 0)
        result = order - o.order;
    return result;
}

This is actually the same as @org.life.java's answer. You might find this one more palatable, though.

@Override
public int compareTo() {
    int result = title.compareTo(o.title);
    if (result == 0)
        result = position - o.position;
    if (result == 0)
        result = order - o.order;
    return result;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文