Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 42 additions & 2 deletions src/arrayMethodSort.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,48 @@
* Implement method Sort
*/
function applyCustomSort() {
[].__proto__.sort2 = function(compareFunction) {
// write code here
[].__proto__.sort2 = function (compareFunction) {
// const newArray = [];
let compare = compareFunction;
const result = [];
const undefinedNumber = [];

if (typeof compare !== 'function') {
compare = (a, b) =>
String(a) > String(b) ? 1 : String(a) < String(b) ? -1 : 0;
}

for (let i = 0; i < this.length - 1; i++) {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This implementation doesn't handle sparse arrays (arrays with empty slots) in the same way as the native Array.prototype.sort. The native method moves all empty slots to the end of the array, whereas your current logic treats them as undefined and includes them in the sorting comparison based on the string 'undefined'.

for (let j = i + 1; j < this.length; j++) {
if (this[i] === undefined) {
continue;
}

if (this[j] === undefined) {
continue;
}
Comment on lines +20 to +26

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This logic for handling undefined values doesn't correctly implement the behavior for sparse arrays. The native sort method moves all empty slots (which are different from undefined values) to the end. Simply skipping these elements leaves them in their relative positions instead of moving them. Consider modifying your comparison logic to treat empty slots as being 'greater' than any defined value, which will cause them to be sorted to the end.


if (compare(this[i], this[j]) > 0) {
[this[i], this[j]] = [this[j], this[i]];
}
}
}

for (let i = 0; i < this.length; i++) {
if (this[i] !== undefined) {
result[result.length] = this[i];
} else {
undefinedNumber[undefinedNumber.length] = this[i];
}
}
Comment on lines +34 to +40

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This block of code correctly separates defined values from undefined/empty ones. However, it treats an empty slot in a sparse array the same as an element that is explicitly undefined. This causes empty slots to be converted to undefined values in the final sorted array, which differs from the native sort behavior.

To fix this, you need to distinguish between these two cases. You can check if an index actually exists in the array using the in operator (e.g., i in this). This will be true for an index with an undefined value but false for an empty slot.


const sorted = [...result, ...undefinedNumber];

for (let i = 0; i < sorted.length; i++) {
this[i] = sorted[i];
}

return this;
};
}

Expand Down