Skip to content

Commit 619cc05

Browse files
Merge branch 'lerp-entry' of https://github.com/alessandrocapialbi/docs into lerp-entry
2 parents 5dbb607 + 78bb0b4 commit 619cc05

File tree

4 files changed

+166
-165
lines changed

4 files changed

+166
-165
lines changed
Lines changed: 75 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,68 +1,111 @@
11
---
22
Title: '.sort()'
3-
Description: 'Returns an array with its items sorted in place.'
3+
Description: 'Sorts the elements of an array in place.'
44
Subjects:
5-
- 'Web Development'
65
- 'Computer Science'
6+
- 'Web Development'
77
Tags:
88
- 'Arrays'
9-
- 'Comparison'
109
- 'Functions'
1110
- 'Methods'
12-
- 'Sort'
13-
- 'Unicode'
11+
- 'Values'
1412
CatalogContent:
1513
- 'introduction-to-javascript'
1614
- 'paths/front-end-engineer-career-path'
1715
---
1816

19-
The **`.sort()`** method returns an array with its items sorted in place.
17+
The JS **`.sort()`** [method](https://www.codecademy.com/resources/docs/javascript/methods) is used to sort the elements of an array in place. By default, it sorts elements as [strings](https://www.codecademy.com/resources/docs/javascript/strings) in ascending order. However, a custom comparison [function](https://www.codecademy.com/resources/docs/javascript/functions) can be provided to achieve more advanced sorting.
2018

21-
## Syntax
19+
## JS `.sort()` Syntax
2220

2321
```pseudo
24-
// No parameters
25-
array.sort();
26-
27-
// With optional function
28-
array.sort((firstElement, secondElement) => { /* function body */ };
22+
arr.sort(compareFn)
2923
```
3024

31-
If the `.sort()` method is used with no arguments, all items with `undefined` values are shifted to the end of the array while the remaining items are converted to [strings](https://www.codecademy.com/resources/docs/javascript/strings) and sorted by [Unicode code point value](https://en.wikipedia.org/wiki/Code_point).
25+
**Parameters:**
3226

33-
An optional function is used to define how items are sorted. This is done by iterating over the `array` and comparing every `firstElement` and `secondElement` in the `/* function body */`.
27+
- `compareFn` (Optional): A function that defines the sort order. It takes two arguments, `a` and `b`, and should return:
28+
- A negative value if `a` should come before `b`
29+
- `0` if `a` and `b` are considered equal
30+
- A positive value if `a` should come after `b`
3431

35-
## Example
32+
**Return value:**
3633

37-
In the following example, the `.sort()` method is applied to two arrays, `letters` and `numbers` (a mix of floats and integers):
34+
JS `.sort()` returns the original array with its elements sorted in the given order.
3835

39-
```js
40-
const letters = ['d', 'b', 'e', 'a', 'c'];
41-
const numbers = [5, 2, 123, 5.01, 43.5];
36+
## Example 1: Sorting Array of Strings Using JS `.sort()`
4237

43-
console.log('Letters: ', letters.sort());
44-
console.log('Numbers: ', numbers.sort());
38+
This example uses JS `.sort()` to sort an array of strings:
39+
40+
```js
41+
const fruits = ['banana', 'apple', 'cherry', 'date'];
42+
fruits.sort();
43+
console.log(fruits);
4544
```
4645

47-
This results in the following output:
46+
Here is the output:
4847

4948
```shell
50-
Letters: [ 'a', 'b', 'c', 'd', 'e' ]
51-
Numbers: [ 123, 2, 43.5, 45, 5, 5.01 ]
49+
[ 'apple', 'banana', 'cherry', 'date' ]
5250
```
5351

54-
The `letters` were sorted in alphabetical order. The items in `numbers` were sorted based on the leading number in the item's value (e.g., their Unicode value). Sorting numerical values more strictly requires a custom comparison function.
52+
## Example 2: Sorting Array of Numbers Using JS `.sort()`
5553

56-
## Codebyte Example
54+
This example uses JS `.sort()` with a comparison function to sort an array of numbers:
5755

58-
The following example showcases how the optional `callback` argument can be used to sort a `numbers` array in ascending and descending order:
56+
```js
57+
const numbers = [10, 5, 20, 1, 100];
58+
numbers.sort((a, b) => a - b);
59+
console.log(numbers);
60+
```
61+
62+
Here is the output:
63+
64+
```shell
65+
[ 1, 5, 10, 20, 100 ]
66+
```
67+
68+
## Codebyte Example: Sorting Array of Objects Using JS `.sort()`
69+
70+
This codebyte example uses JS `.sort()` to sort an array of objects:
5971

6072
```codebyte/javascript
61-
const numbers = [5, 2, 123, 5.01, 43.5];
73+
const users = [
74+
{ name: "Alice", age: 25 },
75+
{ name: "Bob", age: 20 },
76+
{ name: "Charlie", age: 30 }
77+
];
78+
79+
users.sort((a, b) => a.age - b.age);
6280
63-
const ascending = numbers.sort((a, b) => a - b);
64-
console.log("Ascending order: ", ascending);
81+
console.log(users);
82+
```
83+
84+
## Frequently Asked Questions
85+
86+
### 1. How do I sort numbers in descending order using JS `.sort()`?
87+
88+
You can reverse the comparison in your function in JS `.sort()` to sort numbers in descending order:
89+
90+
```js
91+
numbers.sort((a, b) => b - a);
92+
```
93+
94+
### 2. Can I use `sort()` on a string?
6595

66-
const descending = numbers.sort((a, b) => b - a);
67-
console.log("Descending order: ", descending);
96+
No. The `sort()` method is only available on arrays, not on strings. If you want to sort the characters of a string, you first need to convert it into an array (for example, using `split()`), apply `sort()`, and then join it back:
97+
98+
```js
99+
const str = 'hello';
100+
const sorted = str.split('').sort().join('');
101+
console.log(sorted); // "ehllo"
102+
```
103+
104+
### 3. What happens when `sort()` is used in a list (array)?
105+
106+
When you call `sort()` on an array, JavaScript converts the elements to strings by default and compares them in Unicode code point order. This means that numbers may not sort as you expect:
107+
108+
```js
109+
const numbers = [10, 1, 5];
110+
console.log(numbers.sort()); // [1, 10, 5] (string comparison)
68111
```

content/javascript/concepts/sort/sort.md

Lines changed: 0 additions & 112 deletions
This file was deleted.

content/pytorch/concepts/tensor-operations/terms/lerp/lerp.md

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
---
22
Title: '.lerp()'
3-
Description: 'Returns a new tensor as a result of a linear interpolation between two tensors and a scalar or tensor weight.'
3+
Description: 'Returns a tensor containing the linear interpolation of two tensors, controlled by a scalar or tensor weight.'
44
Subjects:
55
- 'Computer Science'
66
- 'Machine Learning'
@@ -14,8 +14,9 @@ CatalogContent:
1414
- 'paths/computer-science'
1515
---
1616

17-
In PyTorch, the **lerp()** function computes the linear interpolation between two [tensors](https://www.codecademy.com/resources/docs/pytorch/tensors) `start` (from the `input`) and `end` with an auxiliary scalar or tensor `weight`. The result is stored in a new tensor `out`. This is mathematically equivalent to compute the function $out_i = start_i + weight_i * (end_i - start_i)$.
18-
The shapes of both tensors must be [broadcastable](https://www.codecademy.com/resources/docs/numpy/array-broadcasting). If `weight` is a tensor, then all the shapes of `start`, `end` and `weight` must be [broadcastable](https://www.codecademy.com/resources/docs/numpy/array-broadcasting).
17+
In PyTorch, the **`.lerp()`** function computes the linear interpolation between an input [tensor](https://www.codecademy.com/resources/docs/pytorch/tensors) (`input`) and an end tensor (`end`), using a scalar or tensor `weight`. This is mathematically equivalent to applying the function $out_i = start_i + weight_i * (end_i - start_i)$.
18+
19+
The shapes of `input`, `end`, and `weight` must be [broadcastable](https://www.codecademy.com/resources/docs/numpy/array-broadcasting).
1920

2021
## Syntax
2122

@@ -27,7 +28,7 @@ torch.lerp(input, end, weight, *, out=None)
2728

2829
- `input`: The input tensor containing the initial points.
2930
- `end`: The ending tensor containing the finishing points.
30-
- `weight`: A float scalar or a tensor with the weight for the interpolation formula.
31+
- `weight`: The shapes of input, end, and weight must be
3132
- `out` (optional): A tensor to store the output. If provided, the result is written to this tensor.
3233

3334
**Return value:**
@@ -36,7 +37,7 @@ Returns a new tensor containing the result given by the interpolation formula.
3637

3738
## Example
3839

39-
In this example, we compute the interpolation between two tensors and a float scalar weight `torch.lerp()`:
40+
The following example shows how to compute the interpolation between two tensors using `torch.lerp()` with a float scalar weight:
4041

4142
```py
4243
import torch

0 commit comments

Comments
 (0)