Cemo
Cemo

Reputation: 5560

Guava way of sorting List according to another list?

I have List<Integer> consisting Ids of my Users. And after a database query, I am retrieving List<User>. I would like to order this list according to first Id list. List<User> may not include some of the Ids. What is the Guava way for sorting this list?

Upvotes: 8

Views: 11900

Answers (6)

Nicolas Duponchel
Nicolas Duponchel

Reputation: 1349

Hardly found a Kotlin answer so I'm posting this here.

list2.sortedBy { Pair(it, list1.indexOf(it.typeOfList1)).second }

And the full TU I wrote to find the answer :


enum class Categorie { UNO, DOS, TRES, QUATROS }
data class Song(val name: String, val categorie: Categorie)

fun main() {
    val categories = arrayListOf(Categorie.QUATROS, Categorie.DOS, Categorie.TRES, Categorie.UNO)
    val songs = arrayListOf(
        Song("s1", Categorie.DOS),
        Song("s2", Categorie.DOS),
        Song("s3", Categorie.UNO),
        Song("s4", Categorie.QUATROS),
        Song("s5", Categorie.TRES),
        Song("s6", Categorie.QUATROS)
    )

    val expectedSortedListOfSongs = arrayListOf(
        Song("s4", Categorie.QUATROS),
        Song("s6", Categorie.QUATROS),
        Song("s1", Categorie.DOS),
        Song("s2", Categorie.DOS),
        Song("s5", Categorie.TRES),
        Song("s3", Categorie.UNO)
    )

    val sortedList: List<Song> = songs.sortedBy { Pair(it, categories.indexOf(it.categorie)).second }

    println("isValid=${sortedList == expectedSortedListOfSongs}")
}

Upvotes: 0

Etienne Neveu
Etienne Neveu

Reputation: 12692

The fully "functional" way, using Guava, would combine Ordering#explicit() with Ordering#onResultOf()

public class UserService {

    @Inject private UserDao userDao;

    public List<User> getUsersWithIds(List<Integer> userIds) {
        List<User> users = userDao.loadUsersWithIds(userIds);
        Ordering<User> orderById = Ordering.explicit(userIds).onResultOf(UserFunctions.getId());
        return orderById.immutableSortedCopy(users);
    }

}

You could declare an anonymous function inline, but I like to declare my functions as static factory methods in a separate class, for a cleaner code (the verbosity of Java's function declarations is hidden in the utility class):

/**
 * Static factory methods to create {@link Function}s for {@link User}s.
 */
public final class UserFunctions {
    private UserFunctions() { /* prevents instantiation */ }

    /**
     * @return a {@link Function} that returns an {@link User}'s id.
     */
    public static Function<User, Integer> getId() {
        return GetIdFunction.INSTANCE;
    }

    // enum singleton pattern
    private enum GetIdFunction implements Function<User, Integer> {
        INSTANCE;

        public Integer apply(User user) {
            return user.getId();
        }
    }

}

As mentioned by @Arne in a comment, this can be simplified in Java 8, using a method reference instead of the UserFunctions class:

public class UserService {

    @Inject private UserDao userDao;

    public List<User> getUsersWithIds(List<Integer> userIds) {
        List<User> users = userDao.loadUsersWithIds(userIds);
        Ordering<User> orderById = Ordering.explicit(userIds).onResultOf(User::getId);
        return orderById.immutableSortedCopy(immutableSortedCopy(users));
    }

}

Upvotes: 14

Jeffrey Bosboom
Jeffrey Bosboom

Reputation: 13653

Here's how to do this with Java 8 lambdas.

List<Integer> ids = ...; List<User> users = ...;
//map ids to their list indices, to avoid repeated indexOf calls
Map<Integer, Integer> rankMap = IntStream.range(0, ids.size()).boxed()
    .collect(Collectors.toMap(ids::get, Function.identity()));
//sort on the id's position in the list
users.sort(Comparator.comparing(u -> rankMap.get(u.id())));

Upvotes: 0

Marcello DeSales
Marcello DeSales

Reputation: 22309

Simpler answer using Google Guava

class Form {
  public Integer index; // for simplicity, no setter/getter included
}

List<Form> forms = ... // list instances, each of each with values for index

// ordering of forms by the ui sort index.
private static final Ordering<Form> sorter = Ordering.natural().onResultOf(new Function<Form, Integer>() {

    @Override
    public Integer apply(Form form) {
       return form.index;
    }
});

private List<Form> sortForms(List<Form> forms) {
    return sorter.sortedCopy(forms);
}

Upvotes: 0

missingfaktor
missingfaktor

Reputation: 92026

Others have already answered your question using Guava. Here is a Functional Java answer.

Please note that you'll have to use immutable data structures from the library in order to avail of all the goodness.

F<User, Integer> indexInIdList = new F<User, Integer>() {
  public Integer f(User u) {
    return idList.elementIndex(Equal.intEqual, u.getId()).toNull();
  }
};
userList.sort(Ord.intOrd.comap(indexInIdList));   

Upvotes: 2

JB Nizet
JB Nizet

Reputation: 691685

I don't think Guava has anything specific to do this. But it's just a matter of writing this comparator:

Collections.sort(userList, new Comparator<User>() {
    @Override
    public int compare(User u1, User u2) {
         int i1 = idList.indexOf(u1.getId());
         int i2 = idList.indexOf(u2.getId());
         return Ints.compare(i1, i2);
    }
}

Now that I think about it, it can also be implemented this way:

final Ordering<Integer> idOrdering = Ordering.explicit(idList);
Collections.sort(userList, new Comparator<User>() {
    @Override
    public int compare(User u1, User u2) {
         return idOrdering.compare(u1.getId(), u2.getId());
    }
}

which is probably more efficient.

Upvotes: 11

Related Questions