If you are not submitting the form, I don't know if it's necessary to wrap in form tag.
considering that you have only 1 text field for input numbers, You need to consider to add some char to use split method in javascript, in this case, I will consider a simple '-'.
First, let's start to separate your elements to be more clearly
<h2><u>Sorting the array in Descending order</u></h2>
<div>
<label for="numbers">Enter the values - </label><br>
<input type="text" name="numbers" id="numbers" ><br><br>
<input type="button" value="Sort" onclick="handleSortNumber()"><br><br>
</div>
<div>
<label for="result">Descending values are - </label><br>
<input type="text" name="result" id="result"><br><br>
</div>
Second, you can keep the inputs elements in a variable
<script>
...
const inputValues = document.getElementById("numbers");
const output = document.getElementById("result");
...
</script>
Now, you can create a handleSortNumber but don't forget that you have a string with values separated with '-' in your input, and now you need to split them, in javascript you can use the method 'split'
function handleSortNumber() {
const splitedValues = inputValues.value.toString().split('-');
...
}
Now, splitedValues will have a array of String and we can to convert each one to a number, and we can use a map to pass for each element.
function handleSortNumber() {
...
const numbers = splitedValues.map(value => Number(value));
...
}
Finally, you can sort a number and insert the result into a output
function handleSortNumber() {
...
const sortedNumbers = numbers.sort(function compare(a, b) {
return b - a;
})
output.value = sortedNumbers.join('-')
}
}
Full code:
<html>
<body>
<h2><u>Sorting the array in Descending order</u></h2>
<div>
<label for="numbers">Enter the values - </label><br>
<input type="text" name="numbers" id="numbers" ><br><br>
<input type="button" value="Sort" onclick="handleSortNumber()"><br><br>
</div>
<div>
<label for="result">Descending values are - </label><br>
<input type="text" name="response" id="result"><br><br>
</div>
<script>
const inputValues = document.getElementById("numbers");
const output = document.getElementById("result");
function handleSortNumber() {
const splitedValues = inputValues.value.toString().split('-');
const numbers = splitedValues.map(value => Number(value));
const sortedNumbers = numbers.sort(function compare(a, b) {
return b - a;
})
output.value = sortedNumbers.join('-')
}
</script>
</body>
</html>