GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — Added array sorting and new ES6 samples · reprogram2098/JavaScript-Lessons@cb8447b

File tree

Expand file treeCollapse file tree
Open diff view settings
Filter options
  • Data_Structures
    • Arrays_New_ES6_1
      • README.md
      • main.js
    • Arrays_New_ES6_2
      • README.md
      • main.js
    • Arrays_Sorting
      • README.md
      • main.js
Expand file treeCollapse file tree
Open diff view settings
Collapse file

‎Data_Structures/Arrays_New_ES6/README.md‎ ‎…ta_Structures/Arrays_New_ES6_1/README.md‎Data_Structures/Arrays_New_ES6/README.md renamed to Data_Structures/Arrays_New_ES6_1/README.md Data_Structures/Arrays_New_ES6/README.md renamed to Data_Structures/Arrays_New_ES6_1/README.md

Copy file name to clipboard
  • Display the source diff
  • Display the rich diff
File renamed without changes.
Collapse file

‎Data_Structures/Arrays_New_ES6/main.js‎ ‎Data_Structures/Arrays_New_ES6_1/main.js‎Data_Structures/Arrays_New_ES6/main.js renamed to Data_Structures/Arrays_New_ES6_1/main.js

Copy file name to clipboard
File renamed without changes.
Collapse file

‎Data_Structures/Arrays_New_ES6_2/README.md‎

Copy file name to clipboard
+3Lines changed: 3 additions & 0 deletions
  • Display the source diff
  • Display the rich diff
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# Title
2+
3+
Description
Collapse file

‎Data_Structures/Arrays_New_ES6_2/main.js‎

Copy file name to clipboard
+19Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
"use strict";
2+
3+
let numbers = [2, 4, 7, 9];
4+
5+
// from() creates a new array from an existing one
6+
let numbers2 = Array.from(numbers);
7+
console.log(numbers2);
8+
9+
// fill() fills an array with specified value
10+
let bacon = [1, 2, 3, 4, 5];
11+
bacon.fill(0);
12+
console.log(bacon);
13+
14+
// we can pass in the starting index as a second parameter
15+
let tuna = [1, 2, 3, 4, 5];
16+
tuna.fill(68, 2);
17+
console.log(tuna);
18+
19+
Collapse file

‎Data_Structures/Arrays_Sorting/README.md‎

Copy file name to clipboard
+3Lines changed: 3 additions & 0 deletions
  • Display the source diff
  • Display the rich diff
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# Title
2+
3+
Description
Collapse file

‎Data_Structures/Arrays_Sorting/main.js‎

Copy file name to clipboard
+63Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
"use strict";
2+
3+
let items = [1, 10, 17, 18, 2, 7, 3, 19, 14, 5];
4+
console.log(items.reverse());
5+
6+
// not what we wanted, because it sorts the numbers as if they are all strings
7+
console.log(items.sort());
8+
9+
console.log('--------------------');
10+
11+
/*
12+
* When the sort() method compares two values, it sends the values to the compare function, and sorts the values
13+
* according to the returned (negative, zero, positive) value.
14+
*
15+
* EXAMPLE:
16+
* When comparing 40 and 100, the sort() method calls the compare function(40,100)
17+
* The function calculates 40-100, and returns -60 (a negative value)
18+
* The sort function will sort 40 as a value lower than 100
19+
* */
20+
items.sort(function (a, b) {
21+
return a - b;
22+
});
23+
console.log(items);
24+
25+
console.log('--------------------');
26+
27+
// reverse sort
28+
items.sort(function (a, b) {
29+
return b - a;
30+
});
31+
console.log(items);
32+
33+
console.log('--------------------');
34+
35+
// can also do this
36+
function compare(a, b) {
37+
if (a < b)
38+
return -1;
39+
if (a > b)
40+
return 1;
41+
return 0;
42+
}
43+
let items2 = [1, 10, 17, 18, 2, 7, 3, 19, 14, 5];
44+
console.log(items2.sort(compare));
45+
46+
console.log('--------------------');
47+
48+
// custom sorting
49+
let friends = [
50+
{name: 'Bucky', age: 50},
51+
{name: 'Sally', age: 40},
52+
{name: 'Tommy', age: 70},
53+
{name: 'Wendy', age: 20},
54+
{name: 'Chris', age: 60}
55+
];
56+
function compareAges(a, b){
57+
if (a.age < b.age)
58+
return -1;
59+
if (a.age > b.age)
60+
return 1;
61+
return 0;
62+
}
63+
console.log(friends.sort(compareAges));

0 commit comments

Comments
0 (0)
You are visitor 01402775 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.