And up next, let's talk about the sorting.
Now, first of all, I want you to understand that when it comes to sorting, we're not changing the
amount of jobs we're going to send back.
We're changing the order.
And this is very, very important.
And basically, the main functionality looks like this.
In the manga we have this sort method which we can chain.
So we have over here this job dot find and we can also add sort.
And the syntax is following.
We pass here the string.
And then for example, if I want to get the latest jobs first.
So basically where the date is, the most recent date I want to go here with minus and then created
at Y.
Created at.
Well, because that's where we have the date.
And again, this is going to return everything in the descending order.
So let me save it here.
Again, I'm not going to change anything about the parameters here.
Let me just send it.
And if we take a look at it, we'll see that this is going to be 20, 23, then zero six then created.
That should be less.
So notice over here it's zero five.
And as you can see, we have this descending order.
Now, if we want the oldest ones, basically, if we want the ascending order, then we just want to
remove this minus.
And again, let's send it and notice the amount of jobs is still the same.
But of course, this is the opposite of the latest date.
So this pretty much is the oldest date that I have in the database.
And also, we can do the same thing for the letters.
For example, if I want to start with A to Z, I just need to pick the property.
In my case, it's going to be position.
And if I want to start with Z to A, then I want to go with the minus and then position.
So let's try it out.
Basically in here I'm going to type position.
Let's again send this should be A to Z.
Check it out.
Now I start with a and of course, if I keep on scrolling.
Now, this is going to be equal to E, And if I try over here with this minus, then of course it's
going to be opposite.
So we start over here with S, but again, we want to make this dynamic.
So how is that going to look like?
Well, for starters, we're not going to hardcode it and we're going to go right after both of these
conditions.
I'll construct a new object.
I'll say const and I'm going to call this sort options.
That's my object.
And as far as the properties, well, I'm just going to provide the values that I'm looking for.
So the first one is going to be newest.
And remember the syntax.
We want to go here with the minus.
Basically, we want to go with descending.
And the property name is Createdat.
Yes, of course, this property name needs to match, otherwise it's not going to make sense.
Then I want to set up the oldest.
And this is going to be just createdat.
Then let's add a comma since that's the syntax.
And then let's set up two properties.
One is going to be for A to Z, and in this case, we're looking for position and looks like I'm just
skipping on the commas for some reason.
And then if I want to go to Z to A, then we want to set it equal to minus and then position, let's
save it.
And in here, basically we want to set up a sort key.
So I'm going to go with const sort key and this is going to be equal to sort options.
And now of course I want to look for the specific parameter.
So essentially I want to grab the sort out of the req dot query and then back where I have the sort
key, I'll use it to get the property value.
So essentially I'm looking for the property dynamically here.
That's why I have the square brackets.
And if for some reason the value is not there, I'll simply go with some default one.
So I'll say sort options and then dot newest.
So if everything fails then basically sort based on the createdat but go in the descending order.
So if I navigate to dot find and if I just add here sort in this case, I want to add that sort key.
So now let's navigate back to get all jobs and let's add that parameter over here.
And again, if we don't provide anything, everything is fine because we still use that newest notice.
We have 20, 23 and then zero six.
Now, if I'm going to provide over here, the oldest now check it out.
I make the request and now I'm actually getting the oldest jobs in my database.
And similarly, if I want a to Z, I'm just going to type here A to Z, That's the value.
So now we start with A and then if we want Z to A, you can probably already guess we're going to start
with that's over here.
Now, of course, on the front end, all of these values are going to be already set in the select input
because if you recall, we already set them up in the utils.
So if I navigate back over here to utils folder, then constants, check it out.
We already have these values, so this is how we're going to set it on the front end.
And with this in place now we can start dealing with the pagination.