javascript sorting program in html

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • aish123
    New Member
    • Jul 2015
    • 11

    javascript sorting program in html

    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...ple ase any body write program for me...i am not getting it...pzz..anybo dy help...
    Last edited by aish123; Jul 13 '15, 08:45 AM. Reason: additonal infoo
  • Dormilich
    Recognized Expert Expert
    • Aug 2008
    • 8694

    #2
    what have you tried?

    Comment

    • aish123
      New Member
      • Jul 2015
      • 11

      #3
      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

      Comment

      • Dormilich
        Recognized Expert Expert
        • Aug 2008
        • 8694

        #4
        getting the textbox value is simple:
        Code:
        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().

        Comment

        • aish123
          New Member
          • Jul 2015
          • 11

          #5
          i want to get multiple numbers from single text box...and sort it manually...pls wilu do d pgm for mee...

          Comment

          • Dormilich
            Recognized Expert Expert
            • Aug 2008
            • 8694

            #6
            np ;)

            Comment

            • aish123
              New Member
              • Jul 2015
              • 11

              #7
              Code:
              <html>
              <head>
              <title>
              Sorting Numbers....!!!!!
              </title>
              <script type="text/javascript">
              var myArray = [];
              function bubblesort()
              {
              var inputValue = document.getElementById("valueField");
              for (var i = 0; i < inputValue.value; ++i)
              {
              inputValue.innerHTML = "";
              myArray[i] = document.getElementById("valueField").value;
              }
              var length=myArray.length;
              var returnArray=new Array(length);
              for (var i =0; i < (arrayToSort.length-1); i = i+1)
              {		
              				
              if ((arrayToSort[i+1]) < (arrayToSort[i]))
              {
              
              var temp = arrayToSort[i+1];
              arrayToSort[i+1] = arrayToSort[i];
              arrayToSort[i] = temp;
              					
              }
              			
              }
              return returnArray;
              
              
              
              </script>
              </head>
              <body>
              <form>
              Enter number of values:
              <input id="valueField" type="number">
              <input id="submitButton" value="Submit" type="button">
              </form>
              
              </body>
              </html>

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

              any corrections and further completion
              Last edited by Dormilich; Jul 13 '15, 03:46 PM. Reason: please use code tags

              Comment

              • Dormilich
                Recognized Expert Expert
                • Aug 2008
                • 8694

                #8
                #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))

                Comment

                • aish123
                  New Member
                  • Jul 2015
                  • 11

                  #9
                  pls..will u write the pgm for me??/plss...

                  Comment

                  • Dormilich
                    Recognized Expert Expert
                    • Aug 2008
                    • 8694

                    #10
                    np ;)

                    Comment

                    • aish123
                      New Member
                      • Jul 2015
                      • 11

                      #11
                      np means??//anybody...pls helppp......... .

                      Comment

                      • Dormilich
                        Recognized Expert Expert
                        • Aug 2008
                        • 8694

                        #12
                        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.

                        Comment

                        • aish123
                          New Member
                          • Jul 2015
                          • 11

                          #13
                          hmm...i dnt know dats y...is there any other place...where i can get the required program

                          Comment

                          • aish123
                            New Member
                            • Jul 2015
                            • 11

                            #14
                            pls tell me how to get input value numbers from one input texbox and get them into array

                            Comment

                            • Dormilich
                              Recognized Expert Expert
                              • Aug 2008
                              • 8694

                              #15
                              erm, I did that on post #4.

                              Comment

                              Working...