I wanted to know how I can order the linkedlist addressBook
in order of the integer/age from youngest to oldest. I am aware that Collections.sort(addressBook);
would organize the list alphabetically, but I do not intend to actually need it in my end product.
import java.io.*;
import java.util.*;
public class ABook
{
public static void main (String args[])
{
LinkedList addressBook = new LinkedList();
Scanner input = new Scanner(System.in);
int n = 0;
do{
System.out.println("Would you like to add a friend? (Say Y or N)");
String reply = input.nextLine();
if(reply.equals("Y"))
{
System.out.println("What is the name of your friend?");
String name = input.nextLine();
System.out.println("What is the age of your friend?");
int age = input.nextInt();
Friend newFriend = new Friend(name,age);
addressBook.add("Name: " + newFriend.name + "; " + "Age: " + newFriend.age);
Collections.sort(addressBook);
System.out.println("This is your Address Book so far: " + addressBook);
n++;
}
...
}
If anyone could let me know, that would be great.
Thank you!