BillGilbert icon

Untitled

BillGilbert | PRO | 05/20/22 09:28:24 AM UTC | 0 ⭐ | 917 👁️ | Never ⏰ | []
JavaScript |

600 B

|

None

|

0 👍

/

0 👎

/**
 * sortStrings - sorts array of string by two criteria "asc" or "desc"
 * @param {string[]} [arr=[]] arr - the array of strings
 * @param {string} [param="asc"] param - the sorting type "asc" or "desc"
 * @returns {string[]}
 */
export function sortStrings(arr, param = `asc`) {
  return param === `desc`
    ? [...arr].sort((a, b) => descCompare(a, b))
    : [...arr].sort((a, b) => ascCompare(a, b));
}
 
function ascCompare(x, y) {
  return x.localeCompare(y, `ru`, { caseFirst: `upper` });
}
 
function descCompare(x, y) {
  return -x.localeCompare(y, `ru`, { caseFirst: `upper` });
}
 

Comments

  •  icon
    01/01/70 12:00:00 AM UTC
    Plain Text |

    0 B

    |

    👍

    /

    👎