-3

I am attempting to add in a users friend list into my application so that they can quickly find people that they follow on twitter, currently I am able to do this but the results come back based on the most recent people that the user followed but not in alphabetical order which is what I want, is there a way that I can essentially take a list of lists and sort the entire list based one one specific element in the list?

EDIT: sorry totally forgot to add in my code, my apologies

long cursor = -1;
        List<User> users = new ArrayList<User>();
        try {
            PagableResponseList<User> userGroups = null;
            do {
                userGroups = twitter.getFriendsList(app.getUserId(), cursor);
                for (User user : userGroups) {
                    users.add(user);
                }
            } while ((cursor = userGroups.getNextCursor()) != 0);

        } catch (TwitterException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
Edmund Rojas
  • 6,376
  • 16
  • 61
  • 92
  • See this question: http://stackoverflow.com/questions/9109890/android-java-how-to-sort-a-list-of-objects-by-a-certain-value-within-the-object – Kuffs Dec 18 '12 at 18:42

2 Answers2

2

Use the Collections.sort() method. You will have to create a custom Comparator for this, but there are many available tutorials on this.
What you basically do is you subclass the Comparator to compare two User objects, and return a result based on the name. If the names are Strings, you can just return user1.name.compareTo(user2.name);, since Strings already have a compare-method.

Jave
  • 31,598
  • 14
  • 77
  • 90
0

Change your List to a Set

Set<User> users = new TreeSet<User>();

This assumes that your Users are unique.

You may have to wrap the User class with your own class so you can write your own Comparator.

Gilbert Le Blanc
  • 50,182
  • 6
  • 67
  • 111