1

I have 2 arraylists, one of type String and the other is a custom class Person.

List names = new ArrayList<String>(); 
List people = new ArrayList<Person>();

Both lists are populated like so:

names.add("bob");
names.add("joe");
names.add("tom");
people.add(new Person("joe")); //Assuming Person has a name property
people.add(new Person("tom"));
people.add(new Person("bob"));

Notice that the same names have been used in both lists, but added in different order. How can I sort the people arraylist in the same order as the names?

Luiggi Mendoza
85.9k16 gold badges159 silver badges353 bronze badges
asked May 18, 2015 at 16:02
10
  • 1
    mkyong.com/java/… may be a good read. You need either a Comparator or make Person Comparable. Commented May 18, 2015 at 16:03
  • Is names guaranteed to be alphabetical? Commented May 18, 2015 at 16:04
  • you need insertion order or alphabetical? Commented May 18, 2015 at 16:04
  • Do you mean "sort" or simply "reorder"? As in, "reorder the people array to match the order in the names" array? Commented May 18, 2015 at 16:04
  • names is not guaranteed alphabetical. Commented May 18, 2015 at 16:05

3 Answers 3

3

Strange requirement, but you can do it by using a Map:

Map<String, Person> personMap = new HashMap<>();
//Assuming people is declared rightfully as List<Person> rather than just List
for (Person people : people) {
 personMap.put(person.getName(), person);
}
List<Person> results = new ArrayList<>();
for (String name : names) {
 if (personMap.containsKey(name)) {
 results.add(personMap.get(name));
 }
}
//in case you need to work with people only
people.clear();
people.addAll(results);
answered May 18, 2015 at 16:10

2 Comments

Any reason for using a separate results list? Why not simply clear people and add directly to that when iterating through names?
@TedHopp well, I was thinking to use a Java 8 solution as proposed here in this other answer I posted and used that way.
2

Since the names array can apparently be in an arbitrary order, the concept of "sorting" isn't very applicable. The most direct approach, I think, is to rebuild the people array from the given names array by using a map. Something like this might work:

void reoderPeople(ArrayList<Person> people, ArrayList<String> names) {
 // first build the map
 Map<String, Person> map = new HashMap<>();
 for (Person p : people) {
 map.add(p.getName(), p);
 }
 // now re-create the people array
 people.clear();
 for (String name : names) {
 people.add(map.get(name));
 }
}

This assumes that there is a one-to-one correspondence between the elements of names and people based on the name. If that's not a correct assumption, then this approach would have to be modified accordingly.

answered May 18, 2015 at 16:11

Comments

0

Use a comparator for sorting people which uses ordering of names list. (Untested code below)

Collections.sort(people, new Comparator<Person>(){
 public int comapre(Person a, Person b) {
 Integer indexA = names.indexOf(a.getName());
 Integer indexB = names.indexOf(b.getName());
 return indexA.compareTo(indexB);
 }
});
answered May 18, 2015 at 16:09

1 Comment

Finally, a comparison-based approach that will will work (provided one changes public int comapre to public int compare). But this seems like a strikingly inefficient compared to building a map.

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.