00:00:00
in this video let's try to work with
00:00:01
sorting so let's say if you have a
00:00:03
collection and you want to sort it how
00:00:04
will you do it so first of all I will
00:00:06
create a list of values I will say list
00:00:08
of uh I will say integers this time and
00:00:11
then let's say nums equal to new at
00:00:14
least right and then we can import this
00:00:17
okay now one thing to observe when you
00:00:19
talk about this type here on the left
00:00:21
hand side there is no compulsion to add
00:00:23
that right inside okay at the start when
00:00:25
generic concept came it was compulsory
00:00:27
for us to assign the values or to set
00:00:30
the type on both the side but after 1.7
00:00:32
they have kept it optional if you
00:00:34
specify the left hand side on the right
00:00:36
hand side you can simply keep empty
00:00:37
bracket that completely works okay so in
00:00:40
this added let me add some values I will
00:00:42
say nums dot add also four and likewise
00:00:46
let me add some elements here
00:00:48
okay so let's say we have these values
00:00:50
here and what our do is of course if I
00:00:52
print this value so it will be printed
00:00:54
as it is and it follows the sequence as
00:00:56
we have seen so if I compile this code
00:00:58
and
00:00:59
run you can see we got the values now
00:01:01
what I want is I want to sort this
00:01:03
values how do I do it now we have a very
00:01:05
special class called collections okay as
00:01:08
I mentioned in the first video uh we
00:01:10
have a collection class as well and this
00:01:12
collection class has lot of methods one
00:01:14
of the method is called sort and in this
00:01:16
sort you can pass the array or address
00:01:18
which you want to sort okay now
00:01:20
collection class belongs to util package
00:01:22
okay and then once you say sort and
00:01:25
after that if you try to print these
00:01:27
values and let's say the output if I run
00:01:29
this code you can see we got three four
00:01:31
seven nine so sorting is actually very
00:01:34
easy in terms of collection here right
00:01:37
now thing is what if you want to sort
00:01:40
this based on your own logic how will
00:01:42
you do it example let's say the values
00:01:44
are 43 uh 31 and then 72 and 9 or let's
00:01:49
say 29. so we got all these values right
00:01:52
and then I want to sort them based on
00:01:55
their last digit not the entire number
00:01:57
because again if you sort this value it
00:02:00
will be sorted based on the values right
00:02:01
I want to sort based on the last digit
00:02:03
so I want 31 to be first and then I want
00:02:06
42 then I want 72 then 43 then 29 so
00:02:09
this one first then this 2 then this 3
00:02:13
and then this nine how do I change a
00:02:15
logic here say by default sort will take
00:02:18
its own logic right in case if you want
00:02:20
to have your own logic in that case what
00:02:23
you can do is we can use something
00:02:25
called a comparator as you can see we
00:02:27
have a comparator here okay how do I use
00:02:30
it so if you talk about comparator let's
00:02:33
try to use comparator here so if I use
00:02:35
compare it vector and if you go there
00:02:38
comparator is basically an interface
00:02:40
okay so how do I use comparator so I can
00:02:43
say comparator comp is equal to new
00:02:45
comparator now this is an interface
00:02:47
right and we know we have multiple ways
00:02:49
of implementing an interface we can
00:02:51
create a class which implements the
00:02:53
interface or we can use something called
00:02:55
an anonymous interface Anonymous class
00:02:57
here
00:02:58
okay this comparator works with integer
00:03:00
and I can specify that here as well
00:03:02
which is integer
00:03:04
in terms of anonymous class such as
00:03:06
composite to specify on the ident side
00:03:07
as well okay we can use this comparator
00:03:10
here and in this I will Define the
00:03:12
method which method we have in
00:03:13
comparator so if you look at this
00:03:15
comparator we have a method called
00:03:17
compare okay that makes sense we can use
00:03:19
compare here
00:03:20
uh what's the Syntax for compare it is
00:03:23
okay not this one
00:03:26
the syntax is int compare and pass the
00:03:28
two values so T is a type here so
00:03:30
integer integer okay so I will say
00:03:33
public int compare
00:03:36
and we have to specify two values
00:03:38
basically one is your I will say ain't I
00:03:42
comma ain't J see when you basically
00:03:45
sort the values you know normally you
00:03:47
compare two values example let's say if
00:03:49
I give you uh four values here two three
00:03:52
let's say we got four we got three we
00:03:54
got one we got six if I want to sort
00:03:57
this at one time I will compare two
00:03:59
values I want it to be ascending order
00:04:00
so smaller value come first and then the
00:04:03
bigger value go second so I will compare
00:04:05
this two values if the first value is
00:04:07
greater than second value I will swap so
00:04:09
after swap it will be four three oh
00:04:11
sorry three four one six then I will
00:04:13
compare these two values uh then it will
00:04:16
be three one four six then you compare
00:04:19
these two values so this will be three
00:04:21
one six or sorry 346 because the first
00:04:25
value is smaller than second value no
00:04:26
swap in this case we have swapped it
00:04:28
because it was a bigger value now once
00:04:31
you got one of three one four six we'll
00:04:34
do this iteration multiple times again
00:04:35
if you compare this two this will be one
00:04:38
three four six then this two it will be
00:04:41
one three four six
00:04:44
now we don't have to do that we don't
00:04:46
have to compare the last two because in
00:04:48
the first iteration you know the biggest
00:04:50
value comes at the end so we got we got
00:04:53
that right so we have to do that only
00:04:54
three times and again for the next
00:04:56
iteration we can say one three I know
00:04:58
this is sorted but still we have to
00:04:59
continue the iteration first compare one
00:05:01
first two one three four six and you're
00:05:04
done
00:05:05
right so this is how basically you sort
00:05:07
the elements the the aim here is when
00:05:09
you sort elements uh this is one of the
00:05:11
algorithm for sorting basically we have
00:05:14
to compare two values and swap that's
00:05:16
basically basic rule right so here as
00:05:18
well when you say you're sorting you
00:05:20
have to just have to specify when to
00:05:21
swap and not to swap so when you compare
00:05:24
I and J
00:05:26
if you say okay now we want to compare
00:05:28
and how do you specify that we have to
00:05:30
swap so if you return one it will swap
00:05:33
if you return minus one it will not swap
00:05:35
it's that simple
00:05:37
okay so what I will do is I will I will
00:05:39
check and I want to check the last
00:05:40
number right so I will check if how do
00:05:42
you get the last number oh mod 10. if I
00:05:46
mod 10 is greater than the J
00:05:50
mod 10 in that case swap else you can
00:05:55
say return
00:05:57
-1
00:05:58
okay so this is how basically you
00:06:00
specify the logic and I can still see an
00:06:03
error here what is the error the compare
00:06:07
method is correct okay there should not
00:06:09
be any error here okay let's see what
00:06:11
happens so what I will do is I will pass
00:06:13
this com object here itself I will say
00:06:16
com
00:06:17
and okay there's still an error
00:06:21
I don't see any reason to get the error
00:06:23
did I forgot any colon or something
00:06:26
things are looking good let's compile
00:06:28
this one let's see what happens compile
00:06:30
there is an issue is not abstract does
00:06:34
not override
00:06:35
okay it is giving an issue with the
00:06:37
compare method is it the right method
00:06:39
when I'm using compare okay that's right
00:06:42
okay do we have to use the glass names
00:06:44
here
00:06:46
integer and integer oh it was because of
00:06:49
the class name my bad yeah so we have to
00:06:51
use a class symbol right because
00:06:52
collection works with the vapor classes
00:06:54
and now I will try to compile this code
00:06:57
and okay compile two times
00:07:01
and run you can see we've got distorted
00:07:03
values we got one two three nine so
00:07:08
basically what we are doing is we are
00:07:09
changing the way it sorts the value if
00:07:11
you want to specify your own logic we
00:07:13
have to pass an object of a comparator
00:07:15
now in this object relation of
00:07:17
comparator you have to specify the logic
00:07:19
on based on which you want to sort this
00:07:21
uh in fact let me give you a challenge
00:07:23
here what you can do is instead of
00:07:24
creating the list of integers create a
00:07:27
list of string and sort this string
00:07:30
array with the help of the length of the
00:07:32
string okay that will that will be
00:07:34
awesome right try that
00:07:36
this is how you sort the value based on
00:07:38
the comparator so what what is
00:07:40
comparator basically so comparator is a
00:07:42
concept or an interface using which you
00:07:44
can specify your own Logic for sorting
00:07:46
okay
00:07:48
that makes sense right now I will do
00:07:50
something else here what I will do now
00:07:52
is instead of having a list of integers
00:07:55
let me have a list of students okay so
00:07:57
what I will do is I will get a class
00:07:59
here called student and
00:08:02
let's say we have int age for the
00:08:05
student and string name and I also want
00:08:08
to have uh two streamers so that I can
00:08:10
print the object as it is so it's a
00:08:12
source action I want to get a two string
00:08:15
method name
00:08:16
because I want to print the student
00:08:18
values as it is and let me also have a
00:08:20
Constructor using which I can assign the
00:08:22
values to them
00:08:23
I will say Source I want a Constructor
00:08:26
which can take both the values okay this
00:08:31
makes sense let me remove this at
00:08:32
override for time being okay so we got
00:08:34
the Constructor we got two strings so
00:08:36
what I will do now is instead of
00:08:37
creating a list of integers let me
00:08:39
create a list of students I will say
00:08:42
student here
00:08:43
and then because that type of student
00:08:45
right and we cannot add numbers now I
00:08:47
will say these are stoods
00:08:50
uh to replace this with stoods
00:08:54
or maybe let's have numbers what's an
00:08:56
issue and then here instead of adding a
00:08:59
number we have to add a student right so
00:09:00
I have to say no student and this will
00:09:02
be value one or let's say we have to
00:09:03
specifying age right let's say age 21
00:09:05
and name is let's say Naveen then let me
00:09:10
just create some more objects here so
00:09:12
let's say age 12 name is let's say John
00:09:17
and then H we got here is 18.
00:09:21
let's say puddle
00:09:23
and we got age 20. let's say this is
00:09:27
Kiran okay so we got this four student
00:09:30
here with different age and different
00:09:31
names now what I would do is I want to
00:09:34
sort them okay timely let me just
00:09:36
comment this section I want to print the
00:09:38
edit as it is and let's see what happens
00:09:40
if you compile and run you can see we
00:09:43
got all the values the only thing is
00:09:44
they are getting a printed in a weird
00:09:46
way I want to print them one by one so
00:09:49
we can use a enhanced for Loop here so I
00:09:51
can say student as Colon's nums and we
00:09:55
should have used studs there
00:09:57
now what I can do is
00:09:59
I will replace nums with stoids okay
00:10:03
this should solve the issue and done
00:10:06
okay so we got from students and I want
00:10:08
to print one student at a time let's see
00:10:11
if that works
00:10:13
compile and run okay so we got all these
00:10:17
values now what I want to do is I want
00:10:19
to sort this values based on their age
00:10:22
how will I do that
00:10:24
in that case I have to say sort okay let
00:10:28
me just try natural sorting the moment
00:10:31
you do that it is not working can you do
00:10:32
that with the sword weather is not
00:10:34
working
00:10:34
okay let me pass the comparator object
00:10:37
the only thing is now this comparator is
00:10:40
not working with integer it is working
00:10:41
with students so it is a student now and
00:10:43
both this object so again we have to
00:10:45
compare two students right so we need to
00:10:47
show it at a time so I will say student
00:10:50
and student
00:10:52
let's say this is I and J now how do we
00:10:54
compare them we are comparing that age
00:10:56
right
00:10:58
so we have to say I dot h is greater
00:11:02
than
00:11:03
J dot h then swap
00:11:06
it's so simple right
00:11:09
since we have worked with the integers
00:11:11
it should work normally and if you want
00:11:12
this code okay so that we got all the
00:11:14
students with the ascending order of age
00:11:16
we got 12 18 20 and 21. this works right
00:11:19
so this is how basically we can use
00:11:21
Compact and we can sort a student as
00:11:23
well but there's one more thing
00:11:25
why we have to pass a comp here why not
00:11:29
if I just say student white is not com
00:11:31
why it's not sorting it's because the
00:11:34
student we have not specified the logic
00:11:36
for the student right because if you go
00:11:38
to the integer class let me just type
00:11:39
integer here just to see if you go to
00:11:42
integer class you can say integer class
00:11:44
Implement something called a comparable
00:11:47
and by that's why by default the sort
00:11:50
works with integers our student is not
00:11:53
implementing comparable so if you want
00:11:55
to have a natural sorting you can also
00:11:57
Implement something called a comparable
00:11:59
the only thing is this compatible
00:12:01
interface has a method called compared
00:12:04
to okay so we have to Define that method
00:12:07
so I can just come back here and say
00:12:09
implement this comparative method
00:12:12
this is the signature
00:12:14
okay and we are comparing this with a
00:12:17
student itself
00:12:19
and let me say this is that okay the the
00:12:22
name of the variable is that will I will
00:12:24
tell you why I've said that but now what
00:12:27
I'm doing is I'm adding a method call
00:12:29
compared to
00:12:30
and I'm implementing something called a
00:12:31
compatible now and then this comparable
00:12:34
actually works with a student we have to
00:12:36
mention that as well now if you go back
00:12:37
you can see there's no error now so sort
00:12:40
method does work with student provided
00:12:42
you are implementing comparable if you
00:12:45
if your students are not compatible how
00:12:46
you can how can you sort them that's
00:12:48
what it says but then if you try to
00:12:51
compile this code and run it will not
00:12:52
work the way we want it you can see this
00:12:54
is not sorted
00:12:55
and if we are not using compared to now
00:12:57
I'm not using comparator what I'm using
00:12:59
is I'm using compared to off compatible
00:13:00
so whatever logic we have written here
00:13:03
the same logic I have to write here
00:13:07
okay now the only thing is we have to
00:13:10
compare two students right
00:13:12
one student is that so we can say this
00:13:14
is that what about the first student the
00:13:17
thing is we are defining this method
00:13:18
inside the class itself right so what we
00:13:20
can do is we can say this remember this
00:13:22
keyword current object so this dot age
00:13:25
the current object the current student
00:13:26
we are comparing the current student
00:13:27
with the other student it's something
00:13:29
like uh when we don't have a compatible
00:13:31
and if you have two students here we
00:13:33
need a third person to compare two
00:13:35
students but now I'm giving this power
00:13:37
to the student itself they can compare
00:13:39
themselves so if I'm comparing with
00:13:40
someone else
00:13:41
I'm this and that is that
00:13:44
okay and let's see if that works now
00:13:47
compile and run oh it works inside we
00:13:50
got 12 18 20 21. so these are basically
00:13:53
we can use compatible and comparator so
00:13:56
you have two choice either you can make
00:13:57
your class implements compatible if you
00:14:00
don't want to do this you can also use
00:14:02
comparator
00:14:03
in fact even if you mention comparable
00:14:05
you can still override the logic by
00:14:07
comparator that's your choice okay uh
00:14:10
one more thing before we close this
00:14:11
let's say I want to use comparator here
00:14:13
if a pass comparator and I want to you I
00:14:15
want to reduce this code remove a Lambda
00:14:17
expression this is a functional
00:14:20
interface we can use Lambda here as well
00:14:22
so what I can do is I can remove this
00:14:24
entire part from here to here I know you
00:14:25
have to watch this video multiple times
00:14:27
to make it work uh I can put an arrow
00:14:30
and we can put this curly brackets as
00:14:33
well
00:14:33
one more thing instead of using IF else
00:14:36
we can also use a title operator here so
00:14:39
the same logic I can just comment this
00:14:40
part I can say return I dot h is greater
00:14:45
than J dot h return one otherwise return
00:14:48
minus 1. so instead of using IF else I'm
00:14:51
using turn it operator we can remove
00:14:53
this part now since we only have one
00:14:55
statement and that to return we don't
00:14:57
need return that is optional we can
00:14:58
remove curly brackets as well and we can
00:15:01
write everything in one line again watch
00:15:03
this video multiple times to understand
00:15:04
what I'm what I'm doing here and also in
00:15:07
the Lambda expression you don't have to
00:15:08
specify the type you can simply say I
00:15:09
and J
00:15:11
and your job is done you can see that
00:15:13
entire comparator went into one line and
00:15:15
that's why functional interface are so
00:15:17
important
00:15:18
let's see if this works and what I will
00:15:19
do is I will just remove this from
00:15:20
comparable
00:15:22
just to see if that works without
00:15:24
implementing compatible Implement run it
00:15:27
works what is comparator if you want to
00:15:30
specify on which logic you want to sort
00:15:31
the elements you can use comparator what
00:15:33
is compatible if you want to give a
00:15:35
power to the class itself to compile
00:15:37
itself or to compare its object to
00:15:39
itself we can use uh compatible there