Java中如何根据对象的字符串值进行排序

发布于 2025-01-02 20:28:32 字数 73 浏览 2 评论 0原文

我有 Java bean 类,我想按 String 类型的一个 bean 属性对这些 bean 列表进行排序。我怎样才能做到这一点?

I have Java bean class and I want to sort list of these beans by one bean attribute that is of String type. How can I do that?

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

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

发布评论

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

评论(3

霞映澄塘 2025-01-09 20:28:32

要么使类型本身实现 Comparable;,通过比较两个字符串来实现compareTo方法,或者实现一个比较器再次通过字符串进行比较。

使用第一种方法,您就可以直接使用Collections.sort();第二个你会使用Collections.sort(collection, new FooComparator());

例如:

public class Foo {
    public String getBar() {
        ...
    }
}

public class FooComparatorByBar implements Comparator<Foo> {
    public int compare(Foo x, Foo y) {
        // TODO: Handle null values of x, y, x.getBar() and y.getBar(),
        // and consider non-ordinal orderings.
        return x.getBar().compareTo(y.getBar());
    }
}

Either make the type itself implement Comparable<Foo>, implementing the compareTo method by comparing the two strings, or implement a Comparator<Foo> which again compares by the strings.

With the first approach, you'd then just be able to use Collections.sort() directly; with the second you'd use Collections.sort(collection, new FooComparator());

For example:

public class Foo {
    public String getBar() {
        ...
    }
}

public class FooComparatorByBar implements Comparator<Foo> {
    public int compare(Foo x, Foo y) {
        // TODO: Handle null values of x, y, x.getBar() and y.getBar(),
        // and consider non-ordinal orderings.
        return x.getBar().compareTo(y.getBar());
    }
}
策马西风 2025-01-09 20:28:32

通过使用自定义比较器?

import java.util.*;

class Bean {
    public final String name;
    public final int value;

    public Bean(final String name, final int value) {
        this.name = name;
        this.value = value;
    }

    @Override
    public String toString() {
        return name + " = " + value;
    }
}

public class SortByProp {
    private static List<Bean> initBeans() {
        return new ArrayList<Bean>(Arrays.asList(
            new Bean("C", 1),
            new Bean("B", 2),
            new Bean("A", 3)
        ));
    }

    private static void sortBeans(List<Bean> beans) {
        Collections.sort(beans, new Comparator<Bean>() {
            public int compare(Bean lhs, Bean rhs){ 
                return lhs.name.compareTo(rhs.name);
            }
        });
    }


    public static void main(String[] args) {
        List<Bean> beans = initBeans();
        sortBeans(beans);
        System.out.println(beans);
    }
}

By using a custom Comparator?

import java.util.*;

class Bean {
    public final String name;
    public final int value;

    public Bean(final String name, final int value) {
        this.name = name;
        this.value = value;
    }

    @Override
    public String toString() {
        return name + " = " + value;
    }
}

public class SortByProp {
    private static List<Bean> initBeans() {
        return new ArrayList<Bean>(Arrays.asList(
            new Bean("C", 1),
            new Bean("B", 2),
            new Bean("A", 3)
        ));
    }

    private static void sortBeans(List<Bean> beans) {
        Collections.sort(beans, new Comparator<Bean>() {
            public int compare(Bean lhs, Bean rhs){ 
                return lhs.name.compareTo(rhs.name);
            }
        });
    }


    public static void main(String[] args) {
        List<Bean> beans = initBeans();
        sortBeans(beans);
        System.out.println(beans);
    }
}
海的爱人是光 2025-01-09 20:28:32

使用Guava,它只是

Collections.sort(list, Ordering.natural().compose(Functions.toStringFunction()));

Using Guava, it's just

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