Is it possible to sort an array with the alphabet string in reverse?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • seanh
    New Member
    • Sep 2007
    • 28

    Is it possible to sort an array with the alphabet string in reverse?

    Hi,

    Does anyone know if its possible to sort an array with the alphabet string in reverse? z-a? The only one I know of is reverse but that doesn't work. Could someone please help I would very much appreciate any help.
    Thank you in advance.
  • dgreenhouse
    Recognized Expert Contributor
    • May 2008
    • 250

    #2
    Are you saying you want to reverse each array element's string value first?

    If so, maybe this will help:

    Code:
    function reverseReverseArray(array_in) {
      for (i=0; i<array_in.length; i++) {
        array_in[i] = reverseString(array_in[i]);
      }
      array_in.reverse();
    }
    
    function reverseString(s_in) {
        var s_out = "";
        var i = s_in.length;
        while (i>0) {
            s_out += s_in.substring(i-1,i);
            i--;
        }
        return s_out;
    }

    Comment

    • seanh
      New Member
      • Sep 2007
      • 28

      #3
      Hi dgreenhouse,

      Thank you so much for your response.

      I'm not really sure what its called but here's an example with code:

      If I input letter a then submit, then letter b and submit..., so I type: a , b, c, d, e

      is it possible to get e, d, c, b, a

      When I put your function which most likely 100% I'm using it wrong I get:
      e, c, a, b, d

      can you show me how to use your function I'm not really sure how the second function works.

      Thank you again,
      -sean.

      Code:
      <html>
        <head>
          <script type="text/javascript" language="javascript">
              var counter=0;
              var alpha =new Array();
            
      	    function reverseAlpha() {
                  var letterFromTxtBox;
                    letterFromTxtBox = document.forms[0][0].value;
                    alpha[counter]=letterFromTxtBox;         
                 lastLetter=alpha[counter];
      	      
      	        if ((alpha )){
                     	   counter++;
      				     alert(" letters: " +  alpha.reverse()+"\n"+ " Last letter typed: " + lastLetter ) ;
                  }      
              } 
       
         function reverseReverseArray(alpha) {
          for (i=0; i<alpha.length; i++) {
             alpha[i] = reverseString(alpha[i]);
           }
          alpha.reverse();
         }
          
         function reverseString(s_in) {
             var s_out = "";
             var i = s_in.length;
             while (i>0) {
                 s_out += s_in.substring(i-1,i);
                 i--;
             }
             return s_out;
         }
         
        </script>
        </head>
      
        <body>
            <form >
                Letter:  <input type="text" name="textBox" /><br />
                   <input type="button"  value="Submit" onClick="reverseAlpha();" name="Submit" />
            </form>
      
        </body>
      </html>

      Comment

      • dgreenhouse
        Recognized Expert Contributor
        • May 2008
        • 250

        #4
        You'll need to store the values then call reverseString() when you've completed taking input.

        Comment

        • seanh
          New Member
          • Sep 2007
          • 28

          #5
          Hi dgreenhouse,
          Thank you so much for your kind reply and generosity with your time.
          -sean

          Comment

          Working...