Sorting numbers and string in javascript


Code:


<!DOCTYPE html >
<html>
<head>
<meta name="generator" content="Bluefish 2.2.5" >
<title>sort numbers and strings</title>
</head>
<body>
<script type="text/javascript">
var array_1 = new Array(10);
var array_2 = new Array(10);
var i, j, temp;
//The length property of an array returns the length of an array (the number of array elements).
//A prompt box is often used if you want the user to input a value before entering a page.
for (i = 0; i < array_1.length; i++) {
    array_1[i] = parseInt(window.prompt("Enter the value"));
}
document.write("Given  values are" + "<br />");
for (i = 0; i < array_1.length; i++) {
    document.write(array_1[i] + "<br />");
}
//sorting
for (i = 0; i < array_1.length; i++) {
    for (j = i + 1; j < array_1.length; j++) {
        if (array_1[i] > array_1[j]) {
            temp = array_1[i];
            array_1[i] = array_1[j];
            array_1[j] = temp;
        }
    }
}
document.write("sorted  values are" + "<br />");
for (i = 0; i < array_1.length; i++) {
    document.write(array_1[i] + "<br />");
}
// string sorting
for (i = 0; i < array_2.length; i++) {
    array_2[i] = (window.prompt("Enter the text"));
}
document.write("Given  text  are" + "<br />");
for (i = 0; i < array_2.length; i++) {
    document.write(array_2[i] + "<br />");
}
for (i = 0; i < array_2.length; i++) {
    for (j = i + 1; j < array_2.length; j++) {
        if (array_2[i] > array_2[j]) {
            temp = array_2[i];
            array_2[i] = array_2[j];
            array_2[j] = temp;
        }
    }
}
document.write("sorted  text  is" + "<br />");
for (i = 0; i < array_2.length; i++) {
    document.write(array_2[i] + "<br />");
}


</script>
</body>
</html>

Output:




OUTPUT SCREEN

No comments:

Post a Comment