• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 433
  • Last Modified:

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>
0
bek
Asked:
bek
  • 3
  • 2
1 Solution
 
BatalfCommented:
OrderType isn't used in the function, You have to implement it into the SortIt function.

Here's an example.

<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
               a=a+"";
               a = a.toLowerCase();
               b=b+"";
               b = b.toLowerCase();
               if (a < b)
                   RetValue = -1
               else
                   if (a > b)
                          RetValue = 1
               if(OrderType=='descending')RetValue = 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>
0
 
bekAuthor Commented:
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?
0
 
BatalfCommented:
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
0
 
bekAuthor Commented:
That is great, Batalf.  Thanks so much!
0
 
BatalfCommented:
Glad I could help! Thanks for the points

Batalf
0

Featured Post

Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now