14.
JavaScript Program for Sorting Values
ALGORITHM:
1. Start the program
2. Enter numbers in the input box (comma separated)
3. Read the entered values from the textbox
4. Convert the input into an array using split(",")
5. Convert each element into integer using parseInt()
6. Perform sorting
Use x - y for ascending order
Use y - x for descending order
7. Display the sorted result and Stop
PROGRAM 14:
<!DOCTYPE html>
<html>
<body>
<h3>Sorting Values</h3>
Enter numbers: <br>
<input id="inp"><br><br>
<button onclick="asc()">Ascending</button>
<button onclick="desc()">Descending</button>
<button onclick="clr()">Clear</button>
<p id="out"></p>
<script>
function asc(){
var a=[Link]("inp").[Link](",");
for(var i=0;i<[Link];i++) a[i]=parseInt(a[i]);
[Link](function(x,y){return x-y});
[Link]("out").innerHTML="Ascending: "+a;
}
function desc(){
var a=[Link]("inp").[Link](",");
for(var i=0;i<[Link];i++) a[i]=parseInt(a[i]);
[Link](function(x,y){return y-x});
[Link]("out").innerHTML="Descending: "+a;
}
function clr(){
[Link]("inp").value="";
[Link]("out").innerHTML="";
}
</script>
</body>
</html>
OUTPUT: