How can I alphabetically in <textarea>? JavaScript sort ()?

I saw several sites that are now in alphabetical order that you have ever placed in their text space. I was wondering what I need to do to add a button for mine that could do this? I assume the JavaScript function is sort (), but I really don't know. All I have seen with this so far is to use it in alphabetical order of arrays. This would be for everything that was entered, in a line, in an open text box. Any ideas?

Thanks for taking the time to read this.

+5
source share
1 answer

splitstring to array, sort , then joinback:

var textarea = document.getElementById("theTextareaId"); // or whatever...
textarea.value = textarea.value.split("\n").sort().join("\n");
+12
source

All Articles