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.
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.
Example. var arr = new Array("orange", "mango", "banana", "sugar"); var sorted = arr. sort(); console.
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");
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With