From 7e09d488eed5960097cd5e812c91c6f4102b3d33 Mon Sep 17 00:00:00 2001 From: "copilot-swe-agent[bot]" <198982749+Copilot@users.noreply.github.com> Date: Tue, 24 Jun 2025 04:56:53 +0000 Subject: [PATCH 1/3] Initial plan for issue From 698e789371f5e73eedfcfb888bdbb74b02c3ddb8 Mon Sep 17 00:00:00 2001 From: "copilot-swe-agent[bot]" <198982749+Copilot@users.noreply.github.com> Date: Tue, 24 Jun 2025 05:00:19 +0000 Subject: [PATCH 2/3] Implement descending sort functionality with comprehensive tests Co-authored-by: Violet-Bora-Lee <6451384+Violet-Bora-Lee@users.noreply.github.com> --- demo.html | 23 +++++++++++++++++++++-- sortDescending.js | 23 +++++++++++++++++++++++ testSort.js | 46 ++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 90 insertions(+), 2 deletions(-) create mode 100644 sortDescending.js create mode 100644 testSort.js diff --git a/demo.html b/demo.html index 320444a..7e8cb88 100644 --- a/demo.html +++ b/demo.html @@ -3,10 +3,10 @@ - FilterRange Function Demo + FilterRange and SortDescending Function Demo -

FilterRange Function Demo

+

FilterRange and SortDescending Function Demo

Open the browser console to see the results.

\ No newline at end of file diff --git a/sortDescending.js b/sortDescending.js new file mode 100644 index 0000000..77d1833 --- /dev/null +++ b/sortDescending.js @@ -0,0 +1,23 @@ +/** + * Sorts an array in descending order (내림차순) + * @param {number[]} arr - The input array + * @returns {number[]} A new array sorted in descending order + */ +function sortDescending(arr) { + return [...arr].sort((a, b) => b - a); +} + +// Export for use in other modules (if using Node.js) +if (typeof module !== 'undefined' && module.exports) { + module.exports = sortDescending; +} + +// Example usage as shown in the problem statement +if (typeof window === 'undefined') { + // Running in Node.js environment + let arr = [5, 2, 1, -10, 8]; + let sorted = sortDescending(arr); + + console.log('Original array:', arr); // Should show [5, 2, 1, -10, 8] (not modified) + console.log('Sorted array (descending):', sorted); // Should show [8, 5, 2, 1, -10] +} \ No newline at end of file diff --git a/testSort.js b/testSort.js new file mode 100644 index 0000000..7abc378 --- /dev/null +++ b/testSort.js @@ -0,0 +1,46 @@ +// Test file for sortDescending function +const sortDescending = require('./sortDescending.js'); + +function test(description, actual, expected) { + const actualStr = JSON.stringify(actual); + const expectedStr = JSON.stringify(expected); + const passed = actualStr === expectedStr; + + console.log(`${passed ? '✅' : '❌'} ${description}`); + if (!passed) { + console.log(` Expected: ${expectedStr}`); + console.log(` Actual: ${actualStr}`); + } + return passed; +} + +function runSortTests() { + console.log('Running sortDescending tests...\n'); + + let allPassed = true; + + // Test case from the problem statement + let arr1 = [5, 2, 1, -10, 8]; + let sorted1 = sortDescending(arr1); + allPassed &= test('Problem statement test case [5, 2, 1, -10, 8]', sorted1, [8, 5, 2, 1, -10]); + allPassed &= test('Original array not modified', arr1, [5, 2, 1, -10, 8]); + + // Basic test cases + allPassed &= test('Basic positive numbers', sortDescending([3, 1, 4, 1, 5]), [5, 4, 3, 1, 1]); + allPassed &= test('Mixed positive and negative', sortDescending([3, -1, 4, -5, 0]), [4, 3, 0, -1, -5]); + allPassed &= test('All negative numbers', sortDescending([-3, -1, -4, -2]), [-1, -2, -3, -4]); + + // Edge cases + allPassed &= test('Empty array', sortDescending([]), []); + allPassed &= test('Single element', sortDescending([42]), [42]); + allPassed &= test('Two elements', sortDescending([1, 3]), [3, 1]); + allPassed &= test('Already sorted descending', sortDescending([5, 3, 1]), [5, 3, 1]); + allPassed &= test('Reverse sorted (ascending)', sortDescending([1, 3, 5]), [5, 3, 1]); + allPassed &= test('All same elements', sortDescending([7, 7, 7]), [7, 7, 7]); + allPassed &= test('Floating point numbers', sortDescending([3.14, 2.71, 1.41]), [3.14, 2.71, 1.41]); + + console.log(`\n${allPassed ? '🎉 All sort tests passed!' : '💥 Some sort tests failed!'}`); + return allPassed; +} + +runSortTests(); \ No newline at end of file From 3878a4990e57d61fc5ec578ea4e1e9cc2c5cb883 Mon Sep 17 00:00:00 2001 From: "copilot-swe-agent[bot]" <198982749+Copilot@users.noreply.github.com> Date: Tue, 24 Jun 2025 05:09:09 +0000 Subject: [PATCH 3/3] Simplify edge case tests to reduce CI pipeline overhead Co-authored-by: Violet-Bora-Lee <6451384+Violet-Bora-Lee@users.noreply.github.com> --- testSort.js | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/testSort.js b/testSort.js index 7abc378..e46305b 100644 --- a/testSort.js +++ b/testSort.js @@ -30,14 +30,10 @@ function runSortTests() { allPassed &= test('Mixed positive and negative', sortDescending([3, -1, 4, -5, 0]), [4, 3, 0, -1, -5]); allPassed &= test('All negative numbers', sortDescending([-3, -1, -4, -2]), [-1, -2, -3, -4]); - // Edge cases + // Essential edge cases allPassed &= test('Empty array', sortDescending([]), []); allPassed &= test('Single element', sortDescending([42]), [42]); - allPassed &= test('Two elements', sortDescending([1, 3]), [3, 1]); - allPassed &= test('Already sorted descending', sortDescending([5, 3, 1]), [5, 3, 1]); - allPassed &= test('Reverse sorted (ascending)', sortDescending([1, 3, 5]), [5, 3, 1]); allPassed &= test('All same elements', sortDescending([7, 7, 7]), [7, 7, 7]); - allPassed &= test('Floating point numbers', sortDescending([3.14, 2.71, 1.41]), [3.14, 2.71, 1.41]); console.log(`\n${allPassed ? '🎉 All sort tests passed!' : '💥 Some sort tests failed!'}`); return allPassed;