1

I have four lists as follows:

    ArrayList<String> tempNameList = new ArrayList<>();
    ArrayList<String> tempPhoneList = new ArrayList<>();
    ArrayList<String> tempAddressList = new ArrayList<>();
    ArrayList<String> tempBirthdayList = new ArrayList<>();

Each list contains records related to the other lists by index. I am trying to sort all the lists in ascending order by the person's Name and Birthday. I have tried using Collections.sort() but it only supports a single list at a time.
How do I sort all the lists by name and birthday?

10
  • 4
    Have you considered using classes? Commented Dec 24, 2021 at 4:48
  • @shmosel do you mean using the List<Object> ? Commented Dec 24, 2021 at 4:49
  • 3
    I mean List<Person> where class Person contains fields name, phone, address and birthday. Commented Dec 24, 2021 at 4:50
  • @shmosel I haven't tried that yet. My code is completed using Lists. How do I sort it using classes? I can change my code Commented Dec 24, 2021 at 4:53
  • 1
    This code shows classic “object phobia”. Create a person (or whatever name makes sense) object. Parallel arrays are not great OO. Commented Dec 24, 2021 at 4:55

1 Answer 1

1

Sorting of multiple lists of the same size is possible following these steps:

  1. Create an array/list of indexes according to the sorting rules (first by names, then by birthdays in appropriate YYYYMMDD format, etc.)
List<String> names  = Arrays.asList("bbb", "aaa", "bbb", "aaa");
List<String> phones = Arrays.asList("1023456", "1324560", "1227890", "1446752");
List<String> bdays  = Arrays.asList("20001122", "19980105", "20010614", "19990507");

int[] indexes = IntStream.range(0, names.size())
    .boxed()
    .sorted(Comparator.comparing(names::get).thenComparing(bdays::get))
    .mapToInt(Integer::intValue)
    .toArray(); // -> [1, 3, 0, 2]
  1. Reorder each of the input lists according to the indexes. A separate method may be created for this:
private static List<String> sort(List<String> list, int[] indexes) {
    return Arrays.stream(indexes).mapToObj(list::get).collect(Collectors.toList());
}
// ...
System.out.println(sort(names,  indexes)); -> [aaa, aaa, bbb, bbb]
System.out.println(sort(phones, indexes)); -> [1324560, 1446752, 1023456, 1227890]
System.out.println(sort(bdays,  indexes)); -> [19980105, 19990507, 20001122, 20010614]

However, it would be more natural to create a container object to combine the values from the different input lists, which has become quite simple after introducing record classes in Java 16, and the sort these objects:

record Person(String name, String phone, String bday) {}

List<Person> persons = IntStream.range(0, names.size())
    .boxed()
    .map(i -> new Person(names.get(i), phones.get(i), bdays.get(i)))
    .sorted(Comparator.comparing(Person::name).thenComparing(Person::bday))
    .collect(Collectors.toList());

persons.forEach(System.out::println);

Output:

Person[name=aaa, phone=1324560, bday=19980105]
Person[name=aaa, phone=1446752, bday=19990507]
Person[name=bbb, phone=1023456, bday=20001122]
Person[name=bbb, phone=1227890, bday=20010614]
Sign up to request clarification or add additional context in comments.

Comments

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.