对集合进行排序

发布于 2024-12-04 07:43:47 字数 76 浏览 0 评论 0原文

我有一个 Persons 对象列表,有两个属性名称和年龄。我想按年龄升序对这个列表进行排序,排序应该是所有名字相同的人都应该在列表的底部。

I have a list of Persons object having two attributes name and age. I want to sort this list by age in ascending order, the sorting should be such that all the persons whose names are same should be at the bottom of the list.

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

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

发布评论

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

评论(4

愁杀 2024-12-11 07:43:47

在您的 Person 类中实现 Compareable 接口(我并没有真正遵循您的逻辑,但我认为您可以将 compareTo 方法更改为您需要的方法):

static class Person implements Comparable<Person> {
    final String name;
    final int age;

    public Person(String name, int age) {
        this.name = name;
        this.age = age;
    }

    @Override
    public int compareTo(Person o) {
        if (o.age == age)
            return o.name.compareTo(name);
        return Integer.valueOf(age).compareTo(Integer.valueOf(o.age));
    }
}

public static void main(String[] args) throws Exception {

    List<Person> persons = new LinkedList<Person>();
    persons.add(new Person("David", 29));
    persons.add(new Person("Linnéa", 27));
    persons.add(new Person("Andreas", 28));
    persons.add(new Person("Christofer", 29));

    Collections.sort(persons);

    for (Person p : persons)
        System.out.println(p.name + " " + p.age);
}

输出:

Linnéa 27
Andreas 28
David 29
Christofer 29

Implement Compareable interface in your Person class (I don't really follow your logic but I think you can change the compareTo method into something you need):

static class Person implements Comparable<Person> {
    final String name;
    final int age;

    public Person(String name, int age) {
        this.name = name;
        this.age = age;
    }

    @Override
    public int compareTo(Person o) {
        if (o.age == age)
            return o.name.compareTo(name);
        return Integer.valueOf(age).compareTo(Integer.valueOf(o.age));
    }
}

public static void main(String[] args) throws Exception {

    List<Person> persons = new LinkedList<Person>();
    persons.add(new Person("David", 29));
    persons.add(new Person("Linnéa", 27));
    persons.add(new Person("Andreas", 28));
    persons.add(new Person("Christofer", 29));

    Collections.sort(persons);

    for (Person p : persons)
        System.out.println(p.name + " " + p.age);
}

Output:

Linnéa 27
Andreas 28
David 29
Christofer 29
朱染 2024-12-11 07:43:47

看一下 Collections.sort 以便根据元素的自然顺序对列表进行排序。列表中的所有元素(即您的情况下的 Person 对象)应实现 类似界面。

Take a look at Collections.sort in order to sort a list according to the natural ordering of its elements. All elements in the list (i.e. Person objects in your case) should implement the Comparable interface.

北恋 2024-12-11 07:43:47

使用 Comparator 而不是使用 ComparableComparator 接口使您能够以任意数量的不同方式对给定集合进行排序。关于 Comparator 接口的另一个方便的事情是,您可以使用它对任何类的实例进行排序 - 甚至
您无法修改的类,这与 Comparable 接口不同,后者强制您更改要对其实例进行排序的类。

Instead of using Comparable use Comparator. The Comparator interface gives you the capability to sort a given collection any number of different ways. The other handy thing about the Comparator interface is that you can use it to sort instances of any class—even
classes you can't modify—unlike the Comparable interface, which forces you to change the class whose instances you want to sort.

云柯 2024-12-11 07:43:47

哦,我的蜘蛛感应很刺痛,让我怀疑是作业:)我只会给你基本的想法。

您可以使用常规比较器对列表进行排序,然后使用另一个循环根据名称对名称进行分组。
使用 lambdaJ 我会这样做:

List persons = new LinkedList(){{
        add(new Person("David", 29));
        add(new Person("Linnéa", 21));
        add(new Person("Linnéa", 27));
        add(new Person("Linnéa", 29));
        add(new Person("Andreas", 28));
        add(new Person("Christofer", 29));
        }};

List sorted = sort(persons, on(Person.class).getAge());
Group groups = group(sorted, by(on(Person.class).getName()));
        
    for(Person person: groups.findAll()){
        System.out.println(person);
    }

输出:

Linnéa: 21
Linnéa: 27
Linnéa: 29
Andreas: 28
David: 29
Christofer: 29

Ooh, my spider-sense is tingling and makes me suspect a homework assignment :) I'll merely give you the basic idea.

You could sort the list with a regular comparator and then group the names according to the name with another loop.
With lambdaJ I would do it like this:

List persons = new LinkedList(){{
        add(new Person("David", 29));
        add(new Person("Linnéa", 21));
        add(new Person("Linnéa", 27));
        add(new Person("Linnéa", 29));
        add(new Person("Andreas", 28));
        add(new Person("Christofer", 29));
        }};

List sorted = sort(persons, on(Person.class).getAge());
Group groups = group(sorted, by(on(Person.class).getName()));
        
    for(Person person: groups.findAll()){
        System.out.println(person);
    }

Output:

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