เราจำเป็นต้องเขียนฟังก์ชัน JavaScript ที่ใช้สตริงตัวพิมพ์เล็กและเรียงลำดับกลับกัน เช่น b ควรมาก่อน a, c ก่อน b เป็นต้น
ตัวอย่าง:
หากสตริงอินพุตเป็น −
const str = "hello";
จากนั้นผลลัพธ์ควรเป็น −
const output = "ollhe";
รหัสสำหรับสิ่งนี้จะเป็น −
const string = 'hello';
const sorter = (a, b) => {
const legend = [-1, 0, 1];
return legend[+(a < b)];
}
const reverseSort = str => {
const strArr = str.split("");
return strArr
.sort(sorter)
.join("");
};
console.log(reverseSort(string)); ต่อไปนี้เป็นผลลัพธ์บนคอนโซล -
ollhe