Search code examples
javasortingtwittertwitter4j

How to sort Name of Screen in twitter?


Here i'm able to get User Screen names and printing with the below code,but how can i sort those name alphabatically.

Twitter twitter = (Twitter) request.getSession().getAttribute("twitter");
String name = (String) request.getSession().getAttribute("name");
long cursor = -1;
IDs ids = twitter.getFollowersIDs(name, cursor);
do {
    for (long id : ids.getIDs()) {
        User user = twitter.showUser(id);
        out.println(user.getName());
    }
} while ((cursor = ids.getNextCursor()) != 0);

This is my code where i'm getting names,how can i sort names.Thank for your help.


Solution

  • Actually a Comparator may be more than you need, I got mixed up with your previous question.

    You can simply collect the names themselves in a list and then sort, e.g.:

    ...
    final List<String> names = new LinkedList<String>();
    do {
        for (...) {
            ...
            names.add(user.getName());
        }
    } while (...);
    
    Collections.sort(names) 
    

    If you inspect names, it will now be sorted.