WEBVTT

00:00.610 --> 00:07.360
Sometimes you want a list of sordid elements haven't cared much so far about whether all our elements

00:07.360 --> 00:10.270
are sorted in some particular order.

00:10.270 --> 00:18.790
Sorting is also a very deep topic and there are many different algorithms you can use to do it but I

00:18.790 --> 00:23.850
want to briefly show you one way you can sort elements using a vector and the sort.

00:23.850 --> 00:27.420
Function of the Siebold plus standard library.

00:27.760 --> 00:38.040
First we need the actual sort function so to do that we just say include algorithm

00:40.950 --> 00:45.850
and before we actually sort it let's just go over what this does.

00:45.960 --> 00:53.550
Basically all we're doing is we're creating a vector of 20 elements initially and we're just giving

00:53.550 --> 00:55.410
it some random value.

00:55.620 --> 00:59.260
See we run it again.

01:03.780 --> 01:06.350
You read it again.

01:06.530 --> 01:12.540
You'll see that it has 20 just random values in a random order.

01:12.680 --> 01:17.710
And so there's no order to the list here.

01:17.710 --> 01:18.920
OK.

01:19.280 --> 01:26.350
So to actually sort everything we call the sort function.

01:26.420 --> 01:36.150
So call it by it's called sort and you just say it back dot gain

01:39.640 --> 01:43.240
it back dot and

01:47.710 --> 01:52.620
so you were using the begin and end functions of the vector.

01:52.720 --> 02:00.040
You can just think of these functions as a cursor to the first element in a cursor to the last element.

02:00.190 --> 02:08.570
We do the beginning and end here because we want to sort the entire vector so that does mean that sorc

02:08.610 --> 02:17.200
can actually do a partial sort of the vector if we wanted but we're not going to do that basically ever

02:17.200 --> 02:19.570
in discours.

02:19.900 --> 02:36.010
So we see here if we actually print out everything so say it's 0 x SPUs and we'll just output every

02:36.100 --> 02:36.890
out.

02:37.120 --> 02:41.880
So it ack.

02:42.130 --> 02:42.320
Or

02:46.450 --> 02:53.800
like that and it's also say this out sorted this

02:56.140 --> 03:03.830
initials so if you run this we should see it should be sorted.

03:03.950 --> 03:06.650
So here's the list.

03:06.650 --> 03:09.300
And here is a sort of list.

03:09.950 --> 03:18.520
And we see that it's in ascending order so things get bigger as you go down.

03:18.920 --> 03:25.350
What if we wanted to change how we wanted to sort this list.

03:25.400 --> 03:31.700
So say going in descending order instead of ascending or how would we do that.

03:31.880 --> 03:37.120
Well we do that by providing sort with a compare function.

03:37.130 --> 03:40.490
So in this case it's actually right out.

03:40.490 --> 03:47.970
So compare function will always return true or false and it will compare.

03:48.230 --> 03:50.140
So just say descending.

03:50.140 --> 03:50.820
Compare

03:54.690 --> 04:00.520
it will compare the two values.

04:00.700 --> 04:06.100
And this type that he that we're using here is the same type as the vector.

04:06.250 --> 04:11.540
So say x 1 and x 2.

04:11.890 --> 04:12.960
Just copy this

04:16.670 --> 04:26.310
piece down here and all this we'll do is we'll say x 1 turn x 1 greater than x 2.

04:30.840 --> 04:38.610
K so this function is for comparing whether the first argument should be before the second argument

04:38.670 --> 04:39.680
or not.

04:40.040 --> 04:44.020
OK so we'll use it here

04:48.310 --> 04:49.670
like this.

04:49.780 --> 04:52.920
And notice this is basically a function pointer.

04:52.930 --> 05:05.370
So the third parameter of this overloaded function sort will actually be a compare function.

05:05.380 --> 05:06.970
So if you run this

05:09.860 --> 05:18.710
see the rain last and we see that it will actually go in descending order and so descending because

05:19.430 --> 05:26.070
X1 should be before x 2 essentially k.

05:26.370 --> 05:31.590
So whatever you had before should be going that way if you want to be in ascending order again we could

05:31.590 --> 05:34.970
just put the sign in around it.

05:36.590 --> 05:40.750
And it's back into ascending order.

05:41.330 --> 05:44.490
This would change a little bit not too much.

05:44.510 --> 05:54.680
If we are using our own types say using a struct because how do you compare one struct type to another

05:54.680 --> 05:55.960
of the same type.

05:56.450 --> 06:05.810
This is less than or equal design doesn't work with our own created types and we will actually compare

06:07.190 --> 06:15.740
structure directly but we might compare say the individual members of the strikes but we'll look at

06:15.740 --> 06:18.620
how to do that in the next lecture.
