-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtranscript.txt
More file actions
280 lines (280 loc) · 4.95 KB
/
transcript.txt
File metadata and controls
280 lines (280 loc) · 4.95 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
0:00
yo so uh what's up today I'm going to be
0:02
talking about the sort method in
0:04
JavaScript the sort method is used to
0:06
sort elements of an array in place
0:09
however with the sort method we sort
0:11
elements as strings in lexicographic
0:14
order not alphabetical if I could
0:16
summarize what lexicographic means it
0:19
means alphabet Plus numbers plus symbols
0:21
as strings let me give you an example
0:23
with just alphabetical characters I have
0:26
an array of fruits to sort this array I
0:28
can take the array name fruits use the
0:31
built-in sort method then
0:35
console.log my array of fruits let me
0:39
expand this and here's my array all
0:42
sorted when sorting we treat any
0:45
elements as strings what if my array
0:47
were numbers I'll add the numbers 1
0:49
through 10 in a somewhat random
0:57
order let's change our array to be
1:00
numbers numbers. sort console.log
1:03
numbers here's the result we have one
1:08
then 10 then the rest seem to be in
1:10
order we're not sorting this array
1:12
numerically we're sorting this array
1:15
lexor graphically we're treating each of
1:17
these numbers as strings that's why we
1:19
have one then 10 10 isn't at the end
1:22
where we expect it would be there's a
1:24
couple extra steps we need to do
1:25
involving sorting
1:27
numbers inside of the sort method we
1:29
have to write a custom comparison
1:31
function this is normally a callback but
1:34
you can write a function expression or
1:35
even better yet an arrow function we'll
1:38
write an arrow function in this case let
1:40
me zoom in with an arrow function we
1:43
first need
1:44
parameters we'll have parameters A and B
1:48
we'll examine two contiguous elements 1
1:51
then 10 10 then 2 two then nine N9 and
1:55
three so on and so forth then we need an
1:58
arrow
2:00
then we'll write a minus B our function
2:04
a minus B will return either a0 or
2:08
positive value depending on what values
2:10
we're examining the sort method will
2:12
sort those values into place depending
2:14
on what the value returned is our array
2:17
should be sorted you can see that 10 is
2:20
at the end instead of right after one
2:23
for reverse order you could say B minus
2:28
a now we're starting with 10 and ending
2:31
with
2:32
one you can even sort objects by a given
2:36
property let's go over that
2:38
example we will create an array of
2:40
people const people equals this will be
2:45
an array will include four objects all
2:48
comma
2:50
separated the first will have a name
2:53
property of
2:58
SpongeBob age
3:00
30 and a GPA
3:04
GPA
3:06
3.0 I'm going to zoom out for the next
3:10
object just going to put that on a new
3:12
line let's copy all these properties
3:15
change name to be Patrick age will be
3:19
37 GPA
3:23
1.5 again for the next
3:26
object we will have
3:28
Squidward
3:30
age
3:32
51 GPA
3:36
2.5 then lastly we'll have Sandy name
3:41
Sandy let's make Sandy
3:45
27 GPA of course it's going to be a
3:49
4.0 I would like to sort this array of
3:51
objects by each person's age we'll take
3:54
our array of objects that's stored
3:56
within
3:58
people use the built-in sort
4:02
method use our Arrow
4:04
function a comma B
4:08
Arrow we'll take parameter a that's
4:11
person one do access their age property
4:15
minus parameter B access their age
4:19
property and that's all you got to do
4:22
now let's
4:24
console.log R
4:28
people
4:33
there we are we have Sandy who's the
4:35
youngest then SpongeBob Patrick
4:38
Squidward for reverse order you'll just
4:41
change a to be B and B to be
4:47
a Squidward Patrick SpongeBob Sandy or
4:51
we can sort by a different property
4:52
let's do GPA this
4:55
time a. GPA b. GPA
5:03
we have Patrick with the lowest GPA
5:05
followed by Squidward SpongeBob Sandy
5:08
then reverse order would be b. GPA minus
5:12
a.
5:13
GPA Sandy SpongeBob Squidward Patrick
5:17
let's try sorting by the name property
5:19
and see what happens
5:22
a.name minus b.n
5:27
name we have SpongeBob Patrick's
5:29
Squidward Sandy but that doesn't seem to
5:31
make sense these aren't in alphabetical
5:33
order if you need to sort by a property
5:36
that contains a string within an object
5:38
there's a different
5:40
formula we will take
5:43
a.name do use the built-in
5:47
local compare
5:49
method compare to b.
5:53
name this method will examine two
5:55
strings for lexicographic
5:58
order now these are all arranged
6:02
lexicographically starting with Patrick
6:04
then Sandy SpongeBob Squidward for
6:07
reverse lexicographic order we would
6:09
take b.n name. local compare then
6:12
examine
6:14
a.name I've just now realized that
6:16
there's a lot of SpongeBob characters
6:18
whose first name starts with an S like
6:20
we have three right here Squidward
6:22
SpongeBob Sandy Patrick all right
6:24
everybody so that is the sort method
6:26
it's used to sort elements of an array
6:28
in place it sorts elements as strings in
6:31
lexicographic order not alphabetical and
6:34
well everybody that is the sort method
6:36
in
6:40
JavaScript