/* Default sorting: An example of sorting a string array
   in Unicode code point order */
const enStrings = ["d", "b", "c", "a"];

enStrings.sort();
console.log(enStrings); // ["a", "b", "c", "d"]

/* An example of sorting numbers in ascending order
   based on their numeric values using a callback function */
const sortedNumbers = [1, 4, 10, 31, 1000];

sortedNumbers.sort((a, b) => a - b);
console.log(sortedNumbers); // [1, 4, 10, 31, 1000]
arr.sort();
arr.sort(compareFunction);
/* Example: Sorting an array of alphabetic characters */
const enStrings = ["d", "b", "c", "a"];

enStrings.sort();
console.log(enStrings); // ["a", "b", "c", "d"]

/* Example: Sorting an array of numeric characters */
const numStrings = [0, 1, 2];

numStrings.sort();
console.log(numStrings); // [0, 1, 2]
arr.sort();
arr.sort(compareFn); // compareFunction (optional): pass a callback function to define the sort order
/**
 * Callback function
 *
 * @param a The first element to compare. This value is never undefined.
 * @param b The second element used for comparison. This value is never undefined.
 *
 * The callback function can be defined as a named function (user-defined function)
 * or as an anonymous function.
 * (Naturally,) any callback function can also be written as an arrow function.
 */
function compareFunction(a, b) {
    // Logic for comparing a and b: must return a comparison result
}
const numbers = [1, 1000, 10, 31];

function compareNumbers(a, b) {
    return a - b;
}

numbers.sort(compareNumbers);

console.log(numbers); // Output: [1, 10, 31, 1000]
const sparseArr = [3, , 1, undefined, 2];
sparseArr.sort();
console.log(sparseArr);
// Example output: [1, 2, 3, undefined, <1 empty slot>]
// Elements with the value undefined or missing elements (sparse array slots)
// are placed at the end of the array after sorting.
const arr = [3, , 1, undefined, 2];

function hasValue(value) {
    // Remove elements whose value is undefined.
    return value !== undefined;
}

/*
 * filter() behaves as follows:
 *
 * 1. Empty slots in a sparse array do not invoke the callback function,
 *    so they are automatically excluded.
 * 2. Elements with the value undefined are excluded because the callback
 *    function returns false for them.
 */
const filteredArr = arr.filter(hasValue);

filteredArr.sort();

console.log(filteredArr); // [1, 2, 3]
const enStrings = ["A", "C", "B"];
enStrings.sort();
console.log(enStrings); // ["A", "B", "C"] 
const enStrings = ["A", "a", "B"];
enStrings.sort();
console.log(enStrings); // ["A", "B", "a"] → the lowercase "a" is sorted after the uppercase "B"
/* Example of sorting a numeric array:
   The numeric elements are compared as strings and sorted
   according to Unicode code point order */
const numbers = [1, 1000, 4, 10, 31];
numbers.sort();

console.log(numbers); // Output: [1, 10, 1000, 31, 4] <= Warning! Sorted as strings
const numbers = [1, 1000, 4, 10, 31];

function compareNumbers(a, b) {
    return a - b;
}

numbers.sort(compareNumbers);

console.log(numbers); // Output: [1, 4, 10, 31, 1000]