Advertisement
BillGilbert

Untitled

May 20th, 2022
714
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /**
  2.  * sortStrings - sorts array of string by two criteria "asc" or "desc"
  3.  * @param {string[]} [arr=[]] arr - the array of strings
  4.  * @param {string} [param="asc"] param - the sorting type "asc" or "desc"
  5.  * @returns {string[]}
  6.  */
  7. export function sortStrings(arr, param = `asc`) {
  8.   return param === `desc`
  9.     ? [...arr].sort((a, b) => descCompare(a, b))
  10.     : [...arr].sort((a, b) => ascCompare(a, b));
  11. }
  12.  
  13. function ascCompare(x, y) {
  14.   return x.localeCompare(y, `ru`, { caseFirst: `upper` });
  15. }
  16.  
  17. function descCompare(x, y) {
  18.   return -x.localeCompare(y, `ru`, { caseFirst: `upper` });
  19. }
  20.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement