Skip to content

Instantly share code, notes, and snippets.

@hernandez87v
Created August 9, 2021 01:06
Show Gist options
  • Select an option

  • Save hernandez87v/4ba51b27210742ccaa666f8d633dbae4 to your computer and use it in GitHub Desktop.

Select an option

Save hernandez87v/4ba51b27210742ccaa666f8d633dbae4 to your computer and use it in GitHub Desktop.
var a = ['c', 'a', 'd', 'b', 'aa'];
var b = [9, 2, 13, 7, 1, 12, 123];
// Sort in ascending lexicographical order using a built-in
a.sort();
b.sort();
console.log('a:', a);
console.log('b:', b);
//-----------------------------------------------------
var a = ['c', 'a', 'd', 'b', 'aa'];
var b = [9, 2, 13, 7, 1, 12, 123];
// Sort in descending lexicographical order using a compare function
a.sort(function (x, y) {
return x < y;
});
b.sort(function (x, y) {
return x < y;
});
console.log('a:', a);
console.log('b:', b);
//-----------------------------------------------------
var a = ['c', 'a', 'd', 'b', 'aa'];
// Sort in descending lexicographical order using a compare arrow function
a.sort((x, y) => x < y);
console.log('a:', a);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment