Link to home
Start Free TrialLog in
Avatar of bek
bekFlag for United States of America

asked on

JavaScript sort function always sorts by ascending order, NOT descending

Hello.

I am trying to allow ascending or descending sorting using the sort function, but my list ALWAYS comes up in ascending order.  I've thrown together a sample program (below) exemplifying my problem.  What am I doing wrong? (I'm a newbie and just starting to learn JavaScript.)

It looks as if my SortIt function always returns the wrong value.

Thanks in advance.

-Brian


<html>
                 <head>
            <script language = "JavaScript">
            <!--
            var InputText = prompt ('Enter a text string below:', ' ');
            var Word = InputText.split (" ")
            var OrderType = prompt ('Enter order type ("ascending" or "descending"):');

            function SortIt (a, b){

                  RetValue = 0

                  if (a < b)
                      RetValue = -1
                  else
                      if (a > b)
                             RetValue = 1

                  return RetValue;
    }

            //-->
            </script>
      </head>

      <body>
            <script language = "JavaScript">
            <!--
            document.writeln ("Original:<br>");
            for (i = 0; i < Word.length; i++)
                   document.writeln (Word[i] + '<br>');


            Word.sort (SortIt);
            document.writeln ("<br>Sorted:<br>");
            for (i = 0; i < Word.length; i++)
             document.writeln (Word[i] + '<br>');
            //-->
            </script>
      </body>
</html>
ASKER CERTIFIED SOLUTION
Avatar of Batalf
Batalf
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of bek

ASKER

Hey, that works.  Thanks very much.

Can you explain a couple of things to me... Why are you doing this:  a=a+"" ?

Also, instead of this:

     if(OrderType=='descending')RetValue = RetValue*-1;

why didn't you say this:

     if(OrderType=='descending')RetValue = -1;

Aren't they the same thing?
a=a+"b":

a=a+"" is there to convert it to a string. If it's considered to be a number by Javascript, the toLowerCase() function will fail.

if(OrderType=='descending')RetValue = -1; :
the sortIt() compares two items from your array, and set RetValue to -1 if "a" is less than "b" and 1 if "a" is higher than "b". That's why use this line:

if(OrderType=='descending')RetValue = RetValue*-1;

to return the oposite result from the function if you're sorting the items in descending order.

If you use

if(OrderType=='descending')RetValue = -1;

and a is less than b, it won't return the value in oposite order, since RetValue allready is -1 from the test above.

Example: The two words "Is" and "This" is sent to the function

a = "is",
b = "This"

This part of your function:

if (a < b)
                   RetValue = -1
               else
                   if (a > b)
                          RetValue = 1

set RetValue to -1 since "is" is smaller than "This". If you now uses the the line

if(OrderType=='descending')RetValue = -1;

RetValue will still be -1 and not the oposite which is 1. That's why I used

if(OrderType=='descending')RetValue = RetValue*-1;

Hope it makes sense. It's not so easy to explain:-)

Batalf
Avatar of bek

ASKER

That is great, Batalf.  Thanks so much!
Glad I could help! Thanks for the points

Batalf