Welcome back.
Let's add another layer to our app, which is to sort the list of our links from top to bottom, and
we can do that fairly easily.
Let's hear.
Instead of returning Hacker News that we have here, we'll create a new function called sort stories.
Vote and we'll give it the Hacker News.
List and we'll create this function up here.
We'll say define sort.
Stories.
Buy votes.
And this will receive the parameter hacker news list.
And we're going to do just a simple sort algorithm here.
So we're going to use the sort function that we have.
We're going to give it the hacker news list.
And let's see what happens.
I'm going to run this.
Oh, and I have to do sort it here.
That's the method.
Let's run this again.
We get an error here, and if we read the error says type error, not supported between instances of
dict and dict.
Hmm.
What does that mean?
Well, we're trying to sort items in a list, but they're all dictionaries.
So Python is saying, hey, I don't understand.
How do you want me to sort it?
Do you want me to sort it by title, by link, by votes?
You got to tell me.
And this is a common pattern with sorted that you'll just have to get used to.
We're going to use a lambda function.
Our second parameter is simply going to be the key that we want to sort by.
And that key that we want to sort by is well votes, right?
So all we going to do is say Lambda.
And say key.
And this key that we're going to sort by is going to be votes.
So that if I save this and run this again.
Look that.
I have everything sorted by votes.
As you can see.
Now, if this is overwhelming to you with the lambda, you can go visit the section of the course where
we talk about lambda here.
But this is just a common pattern that you can remember with sorting.
Whenever you're sorting dictionaries, we use keys and you can give it the syntax with a lambda and
pass it the key.
That you want to sort by.
In our case, it's the votes.
The last thing we want to do is we want to actually have the reverse order.
So the top item in the list should be or the first item in the list should have the highest number of
votes.
So we can simply do that with the third parameter by saying reverse equals true.
Just like that.
Now if I save and run our code.
We see that we have a list of all the top links with the very highest, one of 309 votes right here.
Very, very cool.
Now the exciting part is that you don't even have to visit Hacker News website anymore.
You can be a complete programmer and do cool things like just read the titles here and we can read about,
well, Facebook's $5 Billion penalty.
Due to privacy reasons.
By just selecting this link and let's say you're using terminal, you can just say open selection as
a URL and look at that.
We can read the story directly like that.
Very, very cool.
So now you have a cleaned up, more efficient version of Hacker News that you can read every day.
And all you need to do is just run this script in the morning and get your reading done.
Now, there's many ways that you can expand this project.
You can maybe display this on your own indexed HTML file so that you can actually click on these links.
You can do this with other websites.
But my challenge and exercise to you is this How can we expand this list so that it includes more than
just the first page of Hacker News?
That is, if we go back to Hacker News, so far we've only scraped these 30 links.
But what if I click more here and I want to go to this second page and I want to scrape the first two
pages of Hacker News.
How would I go about doing this in the next video?
I'm going to give you the solution, but I challenge you to figure this out on your own and see how
you might do it.
If you get stuck.
Well, I'll see you in the next video.
Bye bye.