-4

I have a list of names (i.e strings) from user input, and I would like to sort them in alphabetic order

my little project

Nick Parsons
  • 45,728
  • 6
  • 46
  • 64
Peter
  • 1
  • 3
    Welcome to SO! Please post your code as text in the question itself. Links can change over time, rendering the question useless for future visitors who have the same problem as you. Thanks! – ggorlen Nov 26 '19 at 04:48
  • 2
    Just use ```yourArray.sort()``` – Spangle Nov 26 '19 at 04:49
  • 2
    I'm voting this comment down as you need to search first and try to do it by yourself. Just post if you are getting any error or something and need help in that. Don't just post without trying it yourself first. – Abdulhaq Shah Nov 26 '19 at 04:52
  • Thanks for the reply! This is / was my first post, so I promise cure and improvement in the future – Peter Nov 27 '19 at 13:48

3 Answers3

0

You can use something like this:

var fruits = ["Banana", "Orange", "Apple", "Mango"];
fruits.sort()

Also you can write your own method:

sort(function (a, b) {
    if (a > b) {
        return -1;
    }
    if (b > a) {
        return 1;
    }
    return 0;
});
Nick Parsons
  • 45,728
  • 6
  • 46
  • 64
Rahul Sharma
  • 453
  • 3
  • 10
0
var names = ["Banana", "Orange", "Apple", "Mango"];
names.sort();

or

var names = ["Banana", "Orange", "Apple", "Mango"];
names.sort(function(a, b){return a-b})
akash kumar
  • 121
  • 1
  • 6
0
const player = document.getElementById('txt')
const button = document.getElementById('btn')
const team = document.getElementById('team')
const player_array = [];


function newPlayer () {
  player_array.push(player.value);
  shuffle_list();
  //paragraph.textContent.sort
  player.value = ''
  player.focus()
}

function shuffle_list () {
  player_array.sort();
  team.innerHTML = '';
  for (let p of player_array) {
    const paragraph = document.createElement('p')
    team.appendChild(paragraph)
    paragraph.textContent = p;
  }
}

button.addEventListener('click', newPlayer)

Fixed it for you

Shyam Tayal
  • 486
  • 2
  • 13