By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,287 Members | 1,581 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,287 IT Pros & Developers. It's quick & easy.

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

P: 28
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.
Feb 27 '11 #1

✓ answered by dgreenhouse

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

Share this Question
Share on Google+
4 Replies


dgreenhouse
Expert 100+
P: 250
Are you saying you want to reverse each array element's string value first?

If so, maybe this will help:

Expand|Select|Wrap|Line Numbers
  1. function reverseReverseArray(array_in) {
  2.   for (i=0; i<array_in.length; i++) {
  3.     array_in[i] = reverseString(array_in[i]);
  4.   }
  5.   array_in.reverse();
  6. }
  7.  
  8. function reverseString(s_in) {
  9.     var s_out = "";
  10.     var i = s_in.length;
  11.     while (i>0) {
  12.         s_out += s_in.substring(i-1,i);
  13.         i--;
  14.     }
  15.     return s_out;
  16. }
  17.  
Feb 28 '11 #2

P: 28
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.

Expand|Select|Wrap|Line Numbers
  1. <html>
  2.   <head>
  3.     <script type="text/javascript" language="javascript">
  4.         var counter=0;
  5.         var alpha =new Array();
  6.  
  7.         function reverseAlpha() {
  8.             var letterFromTxtBox;
  9.               letterFromTxtBox = document.forms[0][0].value;
  10.               alpha[counter]=letterFromTxtBox;         
  11.            lastLetter=alpha[counter];
  12.  
  13.             if ((alpha )){
  14.                       counter++;
  15.                      alert(" letters: " +  alpha.reverse()+"\n"+ " Last letter typed: " + lastLetter ) ;
  16.             }      
  17.         } 
  18.  
  19.    function reverseReverseArray(alpha) {
  20.     for (i=0; i<alpha.length; i++) {
  21.        alpha[i] = reverseString(alpha[i]);
  22.      }
  23.     alpha.reverse();
  24.    }
  25.  
  26.    function reverseString(s_in) {
  27.        var s_out = "";
  28.        var i = s_in.length;
  29.        while (i>0) {
  30.            s_out += s_in.substring(i-1,i);
  31.            i--;
  32.        }
  33.        return s_out;
  34.    }
  35.  
  36.   </script>
  37.   </head>
  38.  
  39.   <body>
  40.       <form >
  41.           Letter:  <input type="text" name="textBox" /><br />
  42.              <input type="button"  value="Submit" onClick="reverseAlpha();" name="Submit" />
  43.       </form>
  44.  
  45.   </body>
  46. </html>
Feb 28 '11 #3

dgreenhouse
Expert 100+
P: 250
You'll need to store the values then call reverseString() when you've completed taking input.
Mar 1 '11 #4

P: 28
Hi dgreenhouse,
Thank you so much for your kind reply and generosity with your time.
-sean
Mar 1 '11 #5

Post your reply

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