Sometimes you want a list of sordid elements haven't cared much so far about whether all our elements
are sorted in some particular order.
Sorting is also a very deep topic and there are many different algorithms you can use to do it but I
want to briefly show you one way you can sort elements using a vector and the sort.
Function of the Siebold plus standard library.
First we need the actual sort function so to do that we just say include algorithm
and before we actually sort it let's just go over what this does.
Basically all we're doing is we're creating a vector of 20 elements initially and we're just giving
it some random value.
See we run it again.
You read it again.
You'll see that it has 20 just random values in a random order.
And so there's no order to the list here.
OK.
So to actually sort everything we call the sort function.
So call it by it's called sort and you just say it back dot gain
it back dot and
so you were using the begin and end functions of the vector.
You can just think of these functions as a cursor to the first element in a cursor to the last element.
We do the beginning and end here because we want to sort the entire vector so that does mean that sorc
can actually do a partial sort of the vector if we wanted but we're not going to do that basically ever
in discours.
So we see here if we actually print out everything so say it's 0 x SPUs and we'll just output every
out.
So it ack.
Or
like that and it's also say this out sorted this
initials so if you run this we should see it should be sorted.
So here's the list.
And here is a sort of list.
And we see that it's in ascending order so things get bigger as you go down.
What if we wanted to change how we wanted to sort this list.
So say going in descending order instead of ascending or how would we do that.
Well we do that by providing sort with a compare function.
So in this case it's actually right out.
So compare function will always return true or false and it will compare.
So just say descending.
Compare
it will compare the two values.
And this type that he that we're using here is the same type as the vector.
So say x 1 and x 2.
Just copy this
piece down here and all this we'll do is we'll say x 1 turn x 1 greater than x 2.
K so this function is for comparing whether the first argument should be before the second argument
or not.
OK so we'll use it here
like this.
And notice this is basically a function pointer.
So the third parameter of this overloaded function sort will actually be a compare function.
So if you run this
see the rain last and we see that it will actually go in descending order and so descending because
X1 should be before x 2 essentially k.
So whatever you had before should be going that way if you want to be in ascending order again we could
just put the sign in around it.
And it's back into ascending order.
This would change a little bit not too much.
If we are using our own types say using a struct because how do you compare one struct type to another
of the same type.
This is less than or equal design doesn't work with our own created types and we will actually compare
structure directly but we might compare say the individual members of the strikes but we'll look at
how to do that in the next lecture.