Sort in Pas2JS

Top 

Example code : Sort an array in alphabetical order:

var

  fruits : array of String = ["Banana", "Orange", "Apple", "Mango", "Laranja", "Abacate", "Apple"];

begin

  fruits.Sort();

  for var Index := Low(fruits) to High(fruits) do

     WriteLn( fruits[Index] );

Result is:

Abacate

Abacaxi

Apple

Banana

Laranja

Mango

Orange

 

Example code : Sort an array in descending order:

var

  fruits : array of String = ["Banana", "Orange", "Apple", "Mango", "Laranja", "Abacate", "Apple"];

begin

  fruits.Sort();

  fruits.Reverse();

  for var Index := Low(fruits) to High(fruits) do

     WriteLn( fruits[Index] );

Result is:

Orange

Mango

Laranja

Banana

Apple

Abacaxi

Abacate

-------------------------------------------

JS output:

 var fruits = [];

 var fruits = ["Banana","Orange","Apple","Mango","Laranja","Abacate","Apple"].slice();

 var Index = 0;

 fruits.sort();

 fruits.reverse();

      var $temp24;

      for(Index = 0,$temp24 = fruits.length;Index<$temp24;Index++) {

         WriteLn($DIdxR(fruits,Index,""));

 

Note: Sort an array according to the compare function, the comparison function can be omitted for arrays of String, Integer and Float.

 

Code example: Sort number in ascending order.

var points : array of Integer = [40100152510];

begin

  points.Sort();

Result is:

-----------

1

5

10

25

40

100 

-----------