Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I alphabetize within a <textarea>? JavaScript sort()?

I've seen a few sites now that alphabetize what ever you put in their textarea I was wondering what I would have to do in order to add a button to mine that would produce that? I'm guessing the "JavaScript sort()" function, but I really don't know. All I've seen with that so far is using it to alphabetize arrays. This would be for anything entered, per line, in an open textarea. Any ideas?

Thanks for taking the time to read this.

like image 361
user2070685 Avatar asked Feb 14 '13 03:02

user2070685


People also ask

How do I sort text alphabetically in JavaScript?

We can do this in JavaScript by using the sort() method directly or with the compare function. In case you are in a rush, here are the two ways: // order an array of names names. sort(); // order an array of objects with name users.

How do I sort a list alphabetically in typescript?

Example. var arr = new Array("orange", "mango", "banana", "sugar"); var sorted = arr. sort(); console.


1 Answers

split the string into an array, sort it, then join it back together:

var textarea = document.getElementById("theTextareaId"); // or whatever...
textarea.value = textarea.value.split("\n").sort().join("\n");
like image 73
Joseph Silber Avatar answered Oct 20 '22 00:10

Joseph Silber