Search code examples
javatwitter

How to sort twitter tweets with java


I have this code:

for (Status tweet : tweets) {
    y++;
    users.add(new twitterTweets(tweet.getUser().getScreenName(), tweet.getText(), tweet.getCreatedAt()));
    System.out.println(y + ": - " + "@" + tweet.getUser().getScreenName()
            + " - " + tweet.getText() + " - " + tweet.getCreatedAt());
    i++;
}

Collections.sort(users, String.CASE_INSENSITIVE_ORDER);

int a= 0;
for(String temp : users) {
    a++;
    System.out.println(temp);
}

I want to print out tweets, which are sorted by date (ascending, descending order and the sort is CASE_INSENSITIVE) and I also want to print out tweets sorted by screenname and text. I know that I can use Collections.sort, but this does not solve my problem, because when I add screennames in an ArrayList, I can only sort screennames and when I want to print out the tweets sorted by screenname, it will give me wrong answer, because the rest of the tweet (text and date) are not sorted with screenname.

How do I sort tweets by

  • ScreenName
  • Date
  • and Text

(both ascending and descending) and print out sorted tweets with correct author, date and text is basically what I am asking?


Solution

  • You have to pass a Comparator as second parameter to Collections.sort().

    Example: You have a Person class:

    public class Person {
        private final String firstName;
        private final String lastName;
    
        public Person(final String firstName, final String lastName) {
            this.firstName = firstName;
            this.lastName = lastName;
        }
    
        public String getFirstName() {return this.firstName;}  
        public String getLastName()  {return this.lastName;}
    
        @Override
        public String toString() {
            return this.firstName + " " + this.lastName;
        }
    }
    

    Now you can sort a Collection of Person like this:

    public class Main {
        public static void main(String[] args) {
            final List<Person> persons = new LinkedList<>();
            persons.add(new Person("Albert", "Einstein"));
            persons.add(new Person("James", "Bond"));
    
            // sort by lastname, ascending
            Collections.sort(persons, (p1, p2) -> p1.getLastName().toUpper().compareTo(p2.getLastName().toUpper()));        
            persons.forEach(System.out::println);
        }
    }
    

    Output:

    James Bond
    Albert Einstein
    


    For other sort orders, use other fields:

    // firstname, ascending
    (p1, p2) -> p1.getFirstName().toUpper().compareTo(p2.getFirstName().toUpper());
    //             ^ here is now getFirstName()
    

    or for opposing order, switch the objects:

    // lastname, descending
    (p1, p2) -> p2.getLastName().toUpper().compareTo(p1.getLastName().toUpper());
    //          ^ here is now p2           ^ here is now p1
    
    // firstname, descending
    (p1, p2) -> p2.getFirstName().toUpper().compareTo(p1.getFirstName().toUpper());
    //          ^ here is now p2            ^ here is now p1