1/*
2 * Copyright (c) 2023 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 *     http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16const months = ['March', 'Jan', 'Feb', 'Dec'];
17months.sort();
18print(months);
19
20const array1 = [1, 30, 4, 21, 100000];
21array1.sort();
22print(array1);
23const numberArray1 = new Array(40, 1, 5, 200);
24    let res1 = numberArray1.sort();
25print(res1);
26
27const stringArray = ["Blue", "Humpback", "Beluga"];
28const numberArray = [40, 1, 5, 200];
29const numericStringArray = ["80", "9", "700"];
30const mixedNumericArray = ["80", "9", "700", 40, 1, 5, 200];
31
32function compareNumbers(a, b) {
33  return a - b;
34}
35
36stringArray.join(); // 'Blue,Humpback,Beluga'
37stringArray.sort(); // ['Beluga', 'Blue', 'Humpback']
38print(stringArray)
39
40numberArray.join(); // '40,1,5,200'
41numberArray.sort(); // [1, 200, 40, 5]
42numberArray.sort(compareNumbers); // [1, 5, 40, 200]
43print(numberArray)
44
45numericStringArray.join(); // '80,9,700'
46numericStringArray.sort(); // ['700', '80', '9']
47numericStringArray.sort(compareNumbers); // ['9', '80', '700']
48print(numericStringArray)
49
50mixedNumericArray.join(); // '80,9,700,40,1,5,200'
51mixedNumericArray.sort(); // [1, 200, 40, 5, '700', '80', '9']
52mixedNumericArray.sort(compareNumbers); // [1, 5, '9', 40, '80', 200, '700']
53print(mixedNumericArray)
54
55print(["a", "c", , "b"].sort()); // ['a', 'b', 'c', empty]
56print([, undefined, "a", "b"].sort()); // ["a", "b", undefined, empty]
57
58var items = ["réservé", "premier", "cliché", "communiqué", "café", "adieu"];
59items.sort(function (a, b) {
60  return a.localeCompare(b);
61});
62print(items);
63
64const numbers1 = [3, 1, 4, 1, 5];
65const sorted1 = numbers1.sort((a, b) => a - b);
66sorted1[0] = 10;
67print(numbers1[0]); // 10
68
69const numbers = [3, 1, 4, 1, 5];
70const sorted = [...numbers].sort((a, b) => a - b);
71sorted[0] = 10;
72print(numbers[0]); // 3
73
74const arr1 = [3, 1, 4, 1, 5, 9];
75const compareFn = (a, b) => (a > b ? 1 : 0);
76arr1.sort(compareFn);
77print(arr1);
78
79const arr = [3, 1, 4, 1, 5, 9];
80const compareFn1 = (a, b) => (a > b ? -1 : 0);
81arr.sort(compareFn1);
82print(arr);
83
84print(["a", "c", , "b"].sort()); // ['a', 'b', 'c', empty]
85print([, undefined, "a", "b"].sort()); // ["a", "b", undefined, empty]
86