moonelite99

Name Sort

Jul 27th, 2021 (edited)
722
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. var array = ["Kristine", "Inocencia", "Ayako", "Olin", "Nguyet", "Esteban", "Tai"]
  2. var prefix = []
  3. var suffix = []
  4.  
  5. array.forEach(function(value, index) {
  6.     prefix.push(value[0])
  7.     suffix.push(value[value.length - 1].toUpperCase())
  8. })
  9.  
  10. var pos = 0
  11.  
  12. prefix.forEach(function(pre, index1) {
  13.     var count = 0
  14.     suffix.forEach(function(suf, index2) {
  15.         if (pre == suf) {
  16.             count++
  17.         }
  18.     })
  19.  
  20.     if (count == 0) {
  21.         pos = index1
  22.     }
  23. })
  24.  
  25. var res = []
  26. res.push(array[pos])
  27.  
  28. while(res.length < prefix.length) {
  29.     prefix.forEach(function(value, index) {
  30.         if (res[res.length - 1][res[res.length - 1].length - 1].toUpperCase() == value && res.length < prefix.length) {
  31.             res.push(array[index])
  32.         }
  33.     })
  34. }
  35.  
  36. console.log(res)
RAW Paste Data