How to Order Youtube API results

Hello,

I have a page which displays the videos from a channel. I have a search box which filters the videos displayed based on the input. However, the videos are always sorted in reverse order if I use order=relevance

https://www.googleapis.com/youtube/v3/search?key={APKI KEY}&channelId=UC-fR7d7Z2HheSUqFxzTq6Ow&order=relevance&part=snippet&type=video&maxResults=50

I tried changing this to order=date which gives me the newest videos first but at the expense of search not working so well. With order=relevance search works with any matching text but order=date only seems to work with video title.

So, my question is, is it possible to have order=relevance AND sort the display order so newest video is first?

Cheers,
Brian

I don’t think you can do this with the YouTube API. I also don’t think the search will be affected by the sort type. I would create a video and put something like ‘ajlsdf9ih9agoihq3r4g8’ in the description. Then search for that and sort by date and see if it returns. If so - then it would seem that the sort order just makes it seem like the description isn’t being accessed for search.

Wouldn’t it be possible to get the result and then sort afterwards based on the date?

Hi Chad,
Thanks for replying. When I use order by relevance, I can enter a speaker name, ie Andrew and all the videos featuring that name are shown. If I do the same with order by date then I see fewer or no results depending on the search string. Its really odd. As an example.

This shows three results in the browser.

https://www.googleapis.com/youtube/v3/search?q=josey&key=AIzaSyA3xkurWzscAybDKw-rCJ8TxRyiLYOv2Bc&channelId=UC-fR7d7Z2HheSUqFxzTq6Ow&order=relevance&part=snippet&type=video&maxResults=50

The link below shows no results and the only difference is the order=date.

https://www.googleapis.com/youtube/v3/search?q=josey&key=AIzaSyA3xkurWzscAybDKw-rCJ8TxRyiLYOv2Bc&channelId=UC-fR7d7Z2HheSUqFxzTq6Ow&order=date&part=snippet&type=video&maxResults=50

I didn’t know if the results could be sorted afterwards but will look further into that. Wappler has so much functionality, I tend to get lost sometimes.

Thanks and best regards,
Brian

You can just use the sort formatter to sort the resultset.

api1.data.items.sort('snippet.publishedAt')
1 Like

Thank you, Patrick. When you put it like that, it does sound rather simple. Wappler is cool but this community is great :smile:

1 Like