Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
154 views
in Technique[技术] by (71.8m points)

java - Best way to sort 2 array lists?

I have 2 array lists. One is an array list containing birthdays. The other is an array list of names.

I am sorting the array list of dates in descending order by date using

Collections.sort(birthdayList);

I want to have the array list of names be sorted in the same order that the birthday list was.

i.e.

unsorted

bdaylist   namelist

1/20/1980 - Bob

3/15/1970 - Todd

8/25/1990 - Jeff

becomes

sorted

3/15/1970 - Todd

1/20/1980 - Bob

8/25/1990 - Jeff

What would be the most efficient way to do this in Java?

Thank you very much!

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
  • Create a Person class with 2 fields: name and birthday.
  • Put the persons in a list
  • sort with a custom comparator that compares the birthdays

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...