1

okay so I have 2 input parameters:

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

8
  • create a class Person { int age; String name; } and store instances of that in an array. Commented Jan 18, 2018 at 17:19
  • 5
    The clean approach would be to create a class that contains both age and name. Then make that class implement the Comparable interface and make the compareTo method sort by the age field. Commented Jan 18, 2018 at 17:20
  • 2
    It is already wrong to have two different arrays for two pieces of data belonging together. Create a model class containing a name and an age field and create a collection (or an array) for that model type. It would then be easier to sort it. Commented Jan 18, 2018 at 17:20
  • 1
    What do you mean by sorting in the same way? Integers and strings are fondementally different types. Strings could be sorted lexicographiclly, but intgers cannot. Could you please elaborate? At any case I'd suggest reading about generics in java and the comparator class Commented Jan 18, 2018 at 17:21
  • I have one big file with the names and age linked together, but I thought it would be easier to work from a string and int input where the positions match Commented Jan 18, 2018 at 17:21

2 Answers 2

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.

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.

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));
Sign up to request clarification or add additional context in comments.

3 Comments

I think it would help OP if you add the Person class and a small showcase on how to create different persons, adding them to the collection.
I think it the person class would look similar to the student here if I am not mistaken. javatpoint.com/Comparable-interface-in-collection-framework
@Zabuza sure thing ;-)
0

Create a class for Person holding age and name, put all in a sorted set and create a custom 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 Comment

MoreObjects is not part of the standard Java library. You will need Guava for this to work. Instead of taking the age difference I would go for a cleaner and more flexible way with Comparator.comparingInt(Person::getAge).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.