From 83adcd276145e8f686377ad8a76d8f29c2a11909 Mon Sep 17 00:00:00 2001 From: Mohajit Paul <80643937+mohajit2711@users.noreply.github.com> Date: Thu, 15 Dec 2022 06:43:33 -0800 Subject: [PATCH 1/5] Sort Array Given an array of numbers, sort the numbers ascending (1,2,3...) and return the sorted array. In fact, returning any negative value will have the same effect as returning -1 here. Any positive value will do the same as 1. This means we can shorten this function significantly: [3,2,4,1].sort(function comparison(a,b) { return a - b; }); Here, if a is less than b, the result of the subtraction will be negative (placing a first). If b is less than a the result will positive (placing b first). --- arraysort.js | 9 +++++++++ 1 file changed, 9 insertions(+) create mode 100644 arraysort.js diff --git a/arraysort.js b/arraysort.js new file mode 100644 index 0000000..b0366e2 --- /dev/null +++ b/arraysort.js @@ -0,0 +1,9 @@ +function sortUp(array) { + array.sort((a,b) => a-b); +} + +let array = {3,4,51,2,3}; + +sortUp(array); + +module.exports = sortUp; From bb251a6f9a9504c77b037e15ca040f558008e964 Mon Sep 17 00:00:00 2001 From: Mohajit Paul <80643937+mohajit2711@users.noreply.github.com> Date: Thu, 15 Dec 2022 06:54:05 -0800 Subject: [PATCH 2/5] Sort Down Array Sorting Descending In the previous example, we sorted our array ascending: [3,2,4,1].sort(function comparison(a,b) { return a - b; }); A negative result moves a in front of b. A positive result moves b in front of a. Zero keeps the order unchanged. Sorting descending will be the opposite. We want a negative result to move b in front of a and a positive result to move a in front of b. Your Goal: Sort Numbers Descending Given an array of numbers sort them in descending order and return the array. --- sortDownArr.js | 12 ++++++++++++ 1 file changed, 12 insertions(+) create mode 100644 sortDownArr.js diff --git a/sortDownArr.js b/sortDownArr.js new file mode 100644 index 0000000..6c79432 --- /dev/null +++ b/sortDownArr.js @@ -0,0 +1,12 @@ +function sortDown(array) { + return array.sort((a,b) => { + return b-a; + + }); +} + +let array = {3,2,4,1}; + +sortDown(array); + +module.exports = sortDown; From 5f52bdfc5e34e87ba713e49852be3edc618cc464 Mon Sep 17 00:00:00 2001 From: Mohajit Paul <80643937+mohajit2711@users.noreply.github.com> Date: Thu, 15 Dec 2022 06:56:57 -0800 Subject: [PATCH 3/5] String Compare in Array sort Comparing Strings Strings have a built-in method for convienent comparison called localeCompare. 'a'.localeCompare('a'); // 0 'a'.localeCompare('b'); // -1 "apple".localeCompare("abcd"); // 1 The localeCompare method also gives options for things like case, accent sensitivity and language. To learn more see the full documentation. Conveniently, localeCompare returns the numerical values we need to help sort our strings! As shown above, when a string is compared to one that would come after it, the result is -1. When compared to a string that should precede it, the result is 1. Your Goal: Sort Strings Ascending Given an array of strings, sort them in ascending order ('a','b','c'...) and return the sorted array. For debugging purposes, the tests will log your actual results versus the expected test results. --- StringComp.js | 11 +++++++++++ 1 file changed, 11 insertions(+) create mode 100644 StringComp.js diff --git a/StringComp.js b/StringComp.js new file mode 100644 index 0000000..c8908c2 --- /dev/null +++ b/StringComp.js @@ -0,0 +1,11 @@ +function sortStringsUp(array) { + array.sort((a,b) => { + return a.localeCompare(b); + }); +} + +let array = {a,b,b,a,a,b,}; + +console.log(sortStringsUp(array)); + +module.exports = sortStringsUp; From 8dc79c7ec555f9d62a83b0835d0fbb1728697dfb Mon Sep 17 00:00:00 2001 From: Mohajit Paul <80643937+mohajit2711@users.noreply.github.com> Date: Sun, 18 Dec 2022 06:10:57 -0800 Subject: [PATCH 4/5] Strings Ascending Comparing Strings Strings have a built-in method for convienent comparison called localeCompare. 'a'.localeCompare('a'); // 0 'a'.localeCompare('b'); // -1 "apple".localeCompare("abcd"); // 1 The localeCompare method also gives options for things like case, accent sensitivity and language. To learn more see the full documentation. Conveniently, localeCompare returns the numerical values we need to help sort our strings! As shown above, when a string is compared to one that would come after it, the result is -1. When compared to a string that should precede it, the result is 1. Your Goal: Sort Strings Ascending Given an array of strings, sort them in ascending order ('a','b','c'...) and return the sorted array. For debugging purposes, the tests will log your actual results versus the expected test results. --- StringsAscending.js | 7 +++++++ 1 file changed, 7 insertions(+) create mode 100644 StringsAscending.js diff --git a/StringsAscending.js b/StringsAscending.js new file mode 100644 index 0000000..2d15248 --- /dev/null +++ b/StringsAscending.js @@ -0,0 +1,7 @@ +function sortStringsUp(array) { + array.sort((a,b) => { + return a.localeCompare(b); + }); +} + +module.exports = sortStringsUp; From 39db3ce00b2392cfa4802d8f930d6e5c15266301 Mon Sep 17 00:00:00 2001 From: Mohajit Paul <80643937+mohajit2711@users.noreply.github.com> Date: Mon, 19 Dec 2022 07:24:53 -0800 Subject: [PATCH 5/5] Sort Students Sorting by Multiple Properties When it comes to objects, we can sort by their properties. Let's consider some students: const students = [ { id: 1, graduated: true, grade: 86 }, { id: 2, graduated: false, grade: 96 }, { id: 3, graduated: false, grade: 78 }, { id: 4, graduated: true, grade: 96 }, ]; Let's sort this list by two rules in the following priority: Students who Graduated Highest Grades const students = [ { id: 4, graduated: true, grade: 96 }, { id: 1, graduated: true, grade: 86 }, { id: 2, graduated: false, grade: 96 }, { id: 3, graduated: false, grade: 78 }, ]; Now the list shows the graduated students at the top and then it sorts those groups by the highest grades. To compare students, this can be broken up into three scenarios. Your Goal: Sort Students Given an array of students, sort first by graduated then by grade like in the example above. Each object in the students array will have properties id, graduated and grade just like in the example shown above. For debugging purposes, the tests will log your actual results versus the expected test results. --- SortStudents.js | 13 +++++++++++++ 1 file changed, 13 insertions(+) create mode 100644 SortStudents.js diff --git a/SortStudents.js b/SortStudents.js new file mode 100644 index 0000000..b41a2e3 --- /dev/null +++ b/SortStudents.js @@ -0,0 +1,13 @@ +function sortStudents(students) { + students.sort((a,b) => { + if (a.graduated && !b.graduated) { + return -1; + } + if (b.graduated && !a.graduated) { + return 1; + } + return b.grade - a.grade; + }); +} + +module.exports = sortStudents;