473,385 Members | 1,927 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

javascript sorting program in html

11
dear all...please help me...i want a program in java script...which has only a text box in which we enter a numbers or string seperated by comma ....should be sorted and values to be displayed...please any body write program for me...i am not getting it...pzz..anybody help...
Jul 13 '15 #1
20 1790
Dormilich
8,658 Expert Mod 8TB
what have you tried?
Jul 13 '15 #2
aish123
11
actually i dnt know getting how to get values frm single text box and put in array and sort and display...pls help...if u know
Jul 13 '15 #3
Dormilich
8,658 Expert Mod 8TB
getting the textbox value is simple:
Expand|Select|Wrap|Line Numbers
  1. var value = <textbox>.value;
note that <textbox> stands for a reference to the textbox element. (so plain copy/past won’t work)

the functions you’ll likely use next are String.split() and Array.sort().
Jul 13 '15 #4
aish123
11
i want to get multiple numbers from single text box...and sort it manually...pls wilu do d pgm for mee...
Jul 13 '15 #5
Dormilich
8,658 Expert Mod 8TB
np ;)
Jul 13 '15 #6
aish123
11
Expand|Select|Wrap|Line Numbers
  1. <html>
  2. <head>
  3. <title>
  4. Sorting Numbers....!!!!!
  5. </title>
  6. <script type="text/javascript">
  7. var myArray = [];
  8. function bubblesort()
  9. {
  10. var inputValue = document.getElementById("valueField");
  11. for (var i = 0; i < inputValue.value; ++i)
  12. {
  13. inputValue.innerHTML = "";
  14. myArray[i] = document.getElementById("valueField").value;
  15. }
  16. var length=myArray.length;
  17. var returnArray=new Array(length);
  18. for (var i =0; i < (arrayToSort.length-1); i = i+1)
  19. {        
  20.  
  21. if ((arrayToSort[i+1]) < (arrayToSort[i]))
  22. {
  23.  
  24. var temp = arrayToSort[i+1];
  25. arrayToSort[i+1] = arrayToSort[i];
  26. arrayToSort[i] = temp;
  27.  
  28. }
  29.  
  30. }
  31. return returnArray;
  32.  
  33.  
  34.  
  35. </script>
  36. </head>
  37. <body>
  38. <form>
  39. Enter number of values:
  40. <input id="valueField" type="number">
  41. <input id="submitButton" value="Submit" type="button">
  42. </form>
  43.  
  44. </body>
  45. </html>

this is what so far i did...plzzz help....

any corrections and further completion
Jul 13 '15 #7
Dormilich
8,658 Expert Mod 8TB
#1 input fields do not have an innerHTML property. they’re empty elements.

#2 why creating an explicit bubblesort implementation? doesn’t the native sort work for you?

#3 on line 11 you treat the input as a max value. on line 14 you put the same value that many times in an array, i.e. you get an array of size n with the number n as each element. due to that the sorting afterwards does not make any sense.

#4 if your input is not a single number (e.g. a comma-separated list of numbers) your code will bail out due to impossible number conversion (i.e. resulting in NaN (not a number))
Jul 13 '15 #8
aish123
11
pls..will u write the pgm for me??/plss...
Jul 13 '15 #9
Dormilich
8,658 Expert Mod 8TB
np ;)
Jul 14 '15 #10
aish123
11
np means??//anybody...pls helppp..........
Jul 14 '15 #11
Dormilich
8,658 Expert Mod 8TB
it's short for nope (I mean, you’re abbreviating words as well, so why shouldn’t I?). we will not do the work for you, but we will help you with problems you encounter.
Jul 14 '15 #12
aish123
11
hmm...i dnt know dats y...is there any other place...where i can get the required program
Jul 14 '15 #13
aish123
11
pls tell me how to get input value numbers from one input texbox and get them into array
Jul 14 '15 #14
Dormilich
8,658 Expert Mod 8TB
erm, I did that on post #4.
Jul 14 '15 #15
aish123
11
are u telling that more than one number in an input box is invalid??
Jul 14 '15 #16
Dormilich
8,658 Expert Mod 8TB
no, I tell you that your program logic is busted.
Jul 14 '15 #17
aish123
11
Expand|Select|Wrap|Line Numbers
  1. <html>
  2. <head>
  3. <title>
  4. This is the real sorting.....
  5. </title>
  6. </head>
  7. <script type="text/javascript">
  8. var numbers= new array(6);
  9. function swap(i,j)
  10. {
  11.  
  12. }
  13. function selectionSort()
  14. {
  15.     var largest;
  16.     for (i=0; i<5; i++)
  17.     {
  18.         largest=0;
  19.         for (j=1; j< (6 - i); j++)
  20.         {
  21.                 if (numbers[largest] < numbers[j] )
  22.                 {
  23.                         largest=j;
  24.  
  25.                 }
  26.         }
  27.         swap( largest, (5-i) );
  28.  
  29.      }
  30. }
  31.  
  32.  
  33.  
  34. </script>
  35. <body bgcolor=pink>
  36. <form name="form1">
  37. <h1>Input Section</h1>
  38. <table>
  39. <tr>
  40. <th>0</th>
  41. <th>1</th>
  42. <th>2</th>
  43. <th>3</th>
  44. <th>4</th>
  45. <th>5</th>
  46.  
  47. </tr>
  48. <tr>
  49. <td>
  50. <input type="text" name="i0" size="4">
  51. </td>
  52. <td>
  53. <input type="text" name="i1" size="4">
  54. </td>
  55. <td>
  56. <input type="text" name="i2" size="4">
  57. </td>
  58. <td>
  59. <input type="text" name="i3" size="4">
  60. </td>
  61. <td>
  62. <input type="text" name="i4" size="4">
  63. </td>
  64. <td>
  65. <input type="text" name="i5" size="4">
  66. </td>
  67.  
  68. </tr>
  69. </table>
  70.  
  71.  
  72. <input type=button value="Sort" name="selSort"
  73. onClick="sort()">
  74. <h1>Output Section</h1>
  75. Once you click on the sort button above, the numbers in boxes will be sorted
  76. accordingly and then be placed in these boxes.
  77. <table>
  78. <tr>
  79. <td>
  80. <input type="text" name="o0" size="4">
  81. </td>
  82. <td>
  83. <input type="text" name="o1" size="4">
  84. </td>
  85. <td>
  86. <input type="text" name="o2" size="4">
  87. </td>
  88. <td>
  89. <input type="text" name="o3" size="4">
  90. </td>
  91. <td>
  92. <input type="text" name="o4" size="4">
  93. </td>
  94. <td>
  95. <input type="text" name="o5" size="4">
  96. </td>
  97.  
  98. </tr>
  99. </table>
  100.  
  101.  
  102. </form>
  103.  
  104. </body>
  105. </html>
pls tell any corrections...and how to complete the pgm
Jul 14 '15 #18
Dormilich
8,658 Expert Mod 8TB
why do you insist on creating your own sort() implementation?
Jul 14 '15 #19
aish123
11
i hav to do it without built in functions....thatsy............
Jul 14 '15 #20
Dormilich
8,658 Expert Mod 8TB
that would have been nice to know in the first place …

anyways, the first thing to do is analysing the current problems.

- #1 is the conversion from an input to an array
- #2 is the sorting of the array

what you need to do is solve each problem separately instead of trying all at once.

there are different approaches you could do.

- make a temporary page that only concerns with a single problem. i.e. you completely neglect problem #2 while working on #1 (and vice versa)

- mock all other problems while working on one. i.e. use use Array.sort() while working on problem #1 and use a predefined array as input while working on problem #2.
Jul 14 '15 #21

Sign in to post your reply or Sign up for a free account.

Similar topics

5
by: Tim | last post by:
I've been working on this for over a week now, and just can't get this figured out. Hoping one of you gurus can help me out here. I have an xml data island that I'm representing on an htm page as...
4
by: Gareth Gale | last post by:
I'm trying to implement a way of allowing a user to sort a HTML table via Javascript on the client. I've seen lots of samples where single column sorting (asc or desc) is shown, but I'd like nested...
1
by: Mosher | last post by:
Hi all, is there a good way to have a popup window appear using Javascript and the html "map" tag? Here is my code: <map name="navigation"> <area shape="rect" coords="5,57,45,70"...
7
by: ian ward | last post by:
Hello, I want to show the result of some JavaScript on an HTML page. I've had a look at some threads and it seems the conclusion is the following - javascript entities are usable on the...
2
by: ksr | last post by:
Hello, I have a HTML page, which loads an activeX control in the browser. In the <HEADsection, I have javascript similar to the following, <SCRIPT language="JavaScript"> var Index = ""; //...
1
by: myselfrajotia | last post by:
DEAR FRIENDS, I need a software which can calculates the time taken by a sorting program written in C language. or if there is no s/w, then is there any other alternative. plz do help me.
4
by: jessy | last post by:
i need to know if its possible to write the value of a Javascript variable inside HTML tag ..and here's the line : var x=document.getElementById('div1').value; formdiv.innerHTML =...
2
by: LindaDhasan | last post by:
Hi, I am a beginner with javascript.Please help me clarify how to display the result of a javascript function within html underline tag.PFA the code. <html> <head> <script...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.