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.