I am using the following bookmarklet to sort textarea ascending or descending. It is working as expected. But I do not need descending sort option and there is no need of arrows like '↑' or '↓'
I am not able to extract the basic sort function from this code.
javascript: (
function() {
Array.from(document.querySelectorAll('textarea')).map(function(b) {
var a = document.createElement('div');
var d = document.createElement('button');
d.textContent = '↑';
d.addEventListener('click', function(f) {
f.preventDefault();
b.value = Array.from(new Set(b.value.split('\n'))).sort().join('\n')
});
var c = document.createElement('button');
c.textContent = '↓';
c.addEventListener('click', function(f) {
f.preventDefault();
b.value = Array.from(new Set(b.value.split('\n'))).sort().reverse().join('\n')
});
a.appendChild(d);
a.appendChild(c);
b.parentNode.insertBefore(a, b)
})
}
)();
Any help will be appreciated.
Most of the bookmarklet code is used to create the buttons.
If you don't need both sort options, you can remove the 'descending' button and rename the other one to just sort.
If you don't need any button at all, you can remove them so the bookmarklet sorts all text areas directly when clicked.
function oneButton() {
Array.from(document.querySelectorAll('textarea')).map(function(b) {
var a = document.createElement('div');
var d = document.createElement('button');
d.textContent = 'Sort';
d.addEventListener('click', function(f) {
f.preventDefault();
b.value = Array.from(new Set(b.value.split('\n'))).sort().join('\n')
});
a.appendChild(d);
b.parentNode.insertBefore(a, b)
})
}
oneButton() // simulates bookmarklet click
function noButton() {
Array.from(document.querySelectorAll('textarea')).map(function(b) {
b.value = Array.from(new Set(b.value.split('\n'))).sort().join('\n')
});
}
<textarea>B
A</textarea><button onclick="this.previousSibling.value='B\nA'">Reset</button><br>
<!-- noButton function is inserted, you can save this as bookmarklet -->
<a href="javascript:void Array.from(document.querySelectorAll('textarea')).map(function(b){b.value=Array.from(new Set(b.value.split('\n'))).sort().join('\n')});" onclick="void Array.from(document.querySelectorAll('textarea')).map(function(b){b.value=Array.from(new Set(b.value.split('\n'))).sort().join('\n')});" title="You can save this as bookmarklet">'Sort directly' bookmarklet</a>