以相同的方式对两个列表进行排序

时间:2021-07-04 14:01:12

okay so I have 2 input parameters:

好的,所以我有2个输入参数:

String[] Names = {"Martin", "Josef", "John", "Jessica", "Claire"};
int[] Age = {22, 19, 20, 17, 21};

The output I desire is a list that looks like this:

我想要的输出是一个如下所示的列表:

String[] Names = {"Jessica", "Josef", "John", "Claire", "Martin"};
int[] Age = {17, 19, 20, 21, 22};

So I did some research and found that you can sort the age list with array list and collections, however that won't be of any use since I also need the names linked to it.

所以我做了一些研究,发现你可以用数组列表和集合对年龄列表进行排序,但是由于我还需要链接到它的名称,所以没有任何用处。

I was hoping any of you could help me with this :)

我希望你们中的任何人能帮助我:)

2 个解决方案

#1


2  

The ideal solution would be to create a Person class with two fields name and age, therefore making life much easier both in keeping related data together and for maintenance.

理想的解决方案是创建一个具有两个字段名称和年龄的Person类,从而使相关数据保持在一起和维护更加容易。

once the class is constructed with the necessary fields, constructor(s) and getters then you can spin up however many objects required populating it with the necessary data and store this into an array or a list.

一旦用必要的字段,构造函数和getter构造了类,那么你可以启动许多需要用必要数据填充它的对象并将其存储到数组或列表中。

example of the class:

类的例子:

public class Person {
     private String name;
     private int age;

     @Override
     public String toString() {
         return "Person{" +
                 "name='" + name + '\'' +
                 ", age=" + age +
                 '}';
     }

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

     public String getName() {
        return name;
     }

     public int getAge() {
        return age;
     }
}

an array of People, although you can use a list as well:

虽然您也可以使用列表,但是一组人员:

Person[] people = new Person[]{
        new Person("Martin", 22),
        new Person("Josef", 19),
        new Person("John", 20),
        new Person("Jessica", 17),
        new Person("Claire", 21)
};

now you can sort by age and maintain related data like this:

现在您可以按年龄排序并维护相关数据,如下所示:

// if you're using an array
Arrays.sort(people, Comparator.comparingInt(Person::getAge));

// if you're using a list
people.sort(Comparator.comparingInt(Person::getAge));

#2


0  

Create a class for Person holding age and name, put all in a sorted set and create a custom comperator

为持有年龄和名称的Person创建一个类,将all放在一个有序集合中并创建一个自定义comperator

public class F {
    public static void main(String[] args) {
        SortedSet<Person> people = new TreeSet<Person>((o1, o2) -> o1.getAge() - o2.getAge());
        people.add(new Person("foo", 3));
        people.add(new Person("bar", 2));

        System.out.println(people);
    }

    private static class Person {
        private String name;
        private int age;

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

        public String getName() {
            return name;
        }

        public int getAge() {
            return age;
        }

        @Override
        public String toString() {
            return MoreObjects.toStringHelper(this)
                    .add("name", name)
                    .add("age", age)
                    .toString();
        }
    }
}

#1


2  

The ideal solution would be to create a Person class with two fields name and age, therefore making life much easier both in keeping related data together and for maintenance.

理想的解决方案是创建一个具有两个字段名称和年龄的Person类,从而使相关数据保持在一起和维护更加容易。

once the class is constructed with the necessary fields, constructor(s) and getters then you can spin up however many objects required populating it with the necessary data and store this into an array or a list.

一旦用必要的字段,构造函数和getter构造了类,那么你可以启动许多需要用必要数据填充它的对象并将其存储到数组或列表中。

example of the class:

类的例子:

public class Person {
     private String name;
     private int age;

     @Override
     public String toString() {
         return "Person{" +
                 "name='" + name + '\'' +
                 ", age=" + age +
                 '}';
     }

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

     public String getName() {
        return name;
     }

     public int getAge() {
        return age;
     }
}

an array of People, although you can use a list as well:

虽然您也可以使用列表,但是一组人员:

Person[] people = new Person[]{
        new Person("Martin", 22),
        new Person("Josef", 19),
        new Person("John", 20),
        new Person("Jessica", 17),
        new Person("Claire", 21)
};

now you can sort by age and maintain related data like this:

现在您可以按年龄排序并维护相关数据,如下所示:

// if you're using an array
Arrays.sort(people, Comparator.comparingInt(Person::getAge));

// if you're using a list
people.sort(Comparator.comparingInt(Person::getAge));

#2


0  

Create a class for Person holding age and name, put all in a sorted set and create a custom comperator

为持有年龄和名称的Person创建一个类,将all放在一个有序集合中并创建一个自定义comperator

public class F {
    public static void main(String[] args) {
        SortedSet<Person> people = new TreeSet<Person>((o1, o2) -> o1.getAge() - o2.getAge());
        people.add(new Person("foo", 3));
        people.add(new Person("bar", 2));

        System.out.println(people);
    }

    private static class Person {
        private String name;
        private int age;

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

        public String getName() {
            return name;
        }

        public int getAge() {
            return age;
        }

        @Override
        public String toString() {
            return MoreObjects.toStringHelper(this)
                    .add("name", name)
                    .add("age", age)
                    .toString();
        }
    }
}

相关文章