Skip to content

Commit 877356f

Browse files
committed
Fix bug in bucketSort so that a comparison function is now provided to the sort function
The [built-in JavaScript sort method](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/sort) will perform a lexicographical (aka alphabetical) order by converting the elements of the array to strings. This can yield unexpected results when sorting numbers. A comparison function can be passed into the sort method to indicate how array elements should be compared.
1 parent 2691756 commit 877356f

File tree

1 file changed

+1
-1
lines changed

1 file changed

+1
-1
lines changed

Sorts/BucketSort.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ function bucketSort (list, size) {
5353
const sorted = []
5454
// now sort every bucket and merge it to the sorted list
5555
for (let iBucket = 0; iBucket < buckets.length; iBucket++) {
56-
const arr = buckets[iBucket].sort()
56+
const arr = buckets[iBucket].sort((a, b) => a - b)
5757
for (let iSorted = 0; iSorted < arr.length; iSorted++) {
5858
sorted.push(arr[iSorted])
5959
}

0 commit comments

Comments
 (0)