emboole emboole - 5 months ago 10
Java Question

YouTube's response array, sorting programatically

i'm creating an app where i get a youtube search. It is currently working ok, i just want to show better results, by sorting them by title name.

I got a VideoListResponse with 50 items

VideoListResponse videoListResponse = null;
try {
videoListResponse = mYouTubeDataApi.videos()
.list(YOUTUBE_VIDEOS_PART)
.setFields(YOUTUBE_VIDEOS_FIELDS)
.setKey(ApiKey.YOUTUBE_API_KEY)
.setId(TextUtils.join(",", videoIds)).execute();
} catch (IOException e) {
e.printStackTrace();
}


and i want to sort them by title. Let me show an image of the item list:

sort by title

i'm pretty much stuck with this, thank you guys !

Answer

You could retrieve the List within and sort that:

List<Video> items = videoListResponse.getItems();
items.sort(Comparator.comparing(e -> e.getSnippet().getTitle()));