Sort events in database instead of in memory