Java sorting elements of ArrayList and giving all of the information about a given object as an output

Viewed 26

I have created an array in which the integer numbers represent the person's age, and I need them sorted in ascending order, except that I also need for an output to show their other information too (name and last name).

ArrayList<ClassName> list = new ArrayList<>();
ClassName c1 = new ClassName("Name", "LastName", 35);
ClassName c2 = new ClassName("Name", "LastName", 23);
list.add(c1);
list.add(c2);
System.out.println(list);

I used Collections for sorting in this way:

ArrayList<ClassName> list = new ArrayList<>();
ClassName c1 = new ClassName("Name", "LastName", 35);
ClassName c2 = new ClassName("Name", "LastName", 23);
list.add(c1);
list.add(c2);
System.out.println(list); //for showing the whole info

//then i made another array because i needed the sort by integers
ArrayList<Integer> sort = new ArrayList<>(); 
sort.add(c1.age);
sort.add(c2.age);
Collections.sort(sort);
System.out.println("Sorting: " + sort);

Okay so this method will only give me a list of objects before sorting and an Sorting: [23, 35] as an output. But what I need is the full information in order, so what I actually need as an output is this:

Name: Name
Last name: LastName
Age: 23

Name: Name
Last name: LastName
Age: 35

Not the full list and then separate sorting elements. Does anyone have an idea how to do this? Thank you.

1 Answers

if you want this:

Name: Name
Last name: LastName
Age: 23

Name: Name
Last name: LastName
Age: 35

you should Override the toString method of the class ClassName

like this

public String toString() {                                                                                                                                             
    return "Name: " + this.name + "\n" + "Last name: " + this.lastName + "\n" + "Age:" + this.age + "\n";              
}  

and use for loop to print that:

ArrayList<ClassName> list = new ArrayList<>();
ClassName c1 = new ClassName("Name", "LastName", 35);
ClassName c2 = new ClassName("Name", "LastName", 23);
list.add(c1);
list.add(c2);
//System.out.println(list); //for showing the whole info
for ( int i = 0, len = list.size(); i < len; i++ ) {
    System.out.println(list.get(i));
}

ArrayList<Integer> sort = new ArrayList<>(); //then i made another array because i needed the sort by integers
sort.add(c1.age);
sort.add(c2.age);
Collections.sort(sort);
System.out.println("Sorting: " + sort);

Below is the original wrong answer

I ran your code and the result is normal, but it contains an error when i try to compile:

error: cannot find symbol
ArrayList<Integer> sort = new Arraylist<>(); //then i made another array because i needed the sort by integers
                                  ^

This will get a compile exception,I guess, when you wrote the following code, it compiled and ran once:

ArrayList<ClassName> list = new ArrayList<>();
ClassName c1 = new ClassName("Name", "LastName", 35);
ClassName c2 = new ClassName("Name", "LastName", 23);
list.add(c1);
list.add(c2);
System.out.println(list); //for showing the whole info

When you add the following code, you try to run it again, but the compilation fails, and the class file obtained from the last compilation is used when running:

ArrayList<Integer> sort = new Arraylist<>(); 
sort.add(c1.age);
sort.add(c2.age);
Collections.sort(sort);
System.out.println("Sorting: " + sort);

you should write like this: ArrayList<Integer> sort = new ArrayList<>(); , new ArrayList() not new Arraylist

For me it's like this, and it get that Sorting: [23, 35].

Related