Flash / Flex / ActionScript/Array/Sort — различия между версиями

Материал из Web эксперт
Перейти к: навигация, поиск
м (1 версия)
 
(нет различий)

Текущая версия на 08:14, 26 мая 2010

A case-insensitive sort using the Array.CASEINSENSITIVE constant

 
package{
  import flash.display.Sprite;
  
  public class Main extends Sprite{
    public function Main(){
        var aWords:Array = ["o", "S", "a", "C"];
        aWords.sort(Array.CASEINSENSITIVE);
        trace(aWords.toString());  //a,C,o,S
    }
  }
}



Call the sort( ) method passing the bandNameSort compare function:

 

package{
  import flash.display.Sprite;
  
  public class Main extends Sprite{
    public function Main(){
        var bands:Array = ["C","W","L","T","A","r"];
                           
        bands.sort(bandNameSort);
        for(var i:int = 0; i < bands.length; i++) {
            trace(bands[i]);
        }
        
        function bandNameSort(band1:String, band2:String):int{
            band1 = band1.toLowerCase(  );
            band2 = band2.toLowerCase(  );
            if(band1.substr(0, 4) == "the ") {
                band1 = band1.substr(4);
            }
            if(band2.substr(0, 4) == "the ") {
                band2 = band2.substr(4);
            }
            if(band1 < band2) {
                return -1;
            }
            else {
                return 1;
            }
        }
    }
  }
}



Combine the combine the constants using the bitwise OR operator (|)

 
package{
  import flash.display.Sprite;
  
  public class Main extends Sprite{
    public function Main(){
        var words:Array = ["T", "r", "a", "j"];
        words.sort(Array.CASEINSENSITIVE | Array.DESCENDING);
        trace(words);  
    }
  }
}
T,r,j,a



Get the sorted order of an array"s elements without changing the original array: Array.RETURNINDEXEDARRAY

 
package{
  import flash.display.Sprite;
  
  public class Main extends Sprite{
    public function Main(){
        var words:Array = ["t", "r", "a", "j"];
        var indices:Array = words.sort(Array.RETURNINDEXEDARRAY);
        trace(words);   
        trace(indices); 
        for(var i:int = 0; i < words.length; i++) {
          trace(words[indices[i]]);
        }
    }
  }
}
t,r,a,j
2,3,1,0
a
j
r
t



Getting Sorted Indices

 
package{
  import flash.display.Sprite;
  
  public class Main extends Sprite{
    public function Main(){
        var aEmployees:Array = ["A", "P", "C", "H"];
        var aSortedIndices:Array = aEmployees.sort(Array.RETURNINDEXEDARRAY);
        trace(aEmployees.toString());
        trace(aSortedIndices.toString());
        for(var i:Number = 0; i < aSortedIndices.length; i++) {
          trace(aEmployees[aSortedIndices[i]]);
        }
    }
  }
}
/*
A,P,C,H
0,2,3,1
A
C
H
P
*/



Getting the Minimum or Maximum Element

 

package{
  import flash.display.Sprite;
  
  public class Main extends Sprite{
    public function Main(){
        var scores:Array = [10, 4, 15, 8];
        scores.sort(Array.NUMERIC);
        trace("Minimum: " + scores[0]);
        trace("Maximum: " + scores[scores.length - 1]);
    }
  }
}



Randomizing the Elements of an Array

 

package{
  import flash.display.Sprite;
  
  public class Main extends Sprite{
    public function Main(){
        function randomSort(elementA:Object, elementB:Object):Number {
            return Math.random(  ) - .5
        } 
        
        var numbers:Array = new Array(  );
        for(var i:int=0;i<20;i++) {
            numbers[i] = i;
        }
        numbers.sort(randomSort);
        for(var i:int=0;i<numbers.length;i++) {
            trace(numbers[i]);
        }
        
    }
  }
}



Reverse the order of the elements in an array

 
package{
  import flash.display.Sprite;
  
  public class Main extends Sprite{
    public function Main(){
        var words:Array = ["t", "r", "a", "j"];
        words.reverse(  );
        trace(words); //j,a,r,t
    }
  }
}



Sort an array in descending order using the Array.DESCENDING constant:

 
package{
  import flash.display.Sprite;
  
  public class Main extends Sprite{
    public function Main(){
        var words:Array = ["t", "r", "a", "j"];
        words.sort(Array.DESCENDING);
        trace(words); //t,r,j,a
    }
  }
}



Sorting and Testing for Unique Values

 
package{
  import flash.display.Sprite;
  
  public class Main extends Sprite{
    public function Main(){
        var aEmployees:Array = ["A", "P", "C", "H"];
        if(aEmployees.sort(Array.UNIQUESORT) != 0) {
          trace(aEmployees.toString());
        }
        else {
          trace("Array has duplicate elements, and has not been sorted. ");
          trace(aEmployees.toString());
        }
    }
  }
}
// A,C,H,P



Sorting Arrays

 
//sort() sorts regular, single dimension arrays.
//sortOn() sorts arrays of associative arrays based on one of the keys of the associative arrays.
//reverse() reverses the order of the elements.

package{
  import flash.display.Sprite;
  
  public class Main extends Sprite{
    public function Main(){
        var aEmployees:Array = ["A", "P", "C", "H"];
        aEmployees.sort();
        trace(aEmployees.toString());
    }
  }
}
A,C,H,P



Sorting in Descending Order

 
package{
  import flash.display.Sprite;
  
  public class Main extends Sprite{
    public function Main(){
        var aEmployees:Array = ["A", "P", "C", "H"];
        aEmployees.sort(Array.DESCENDING);
        trace(aEmployees.toString()); //P,H,C,A
    }
  }
}



Sorting Numerically: Array.NUMERIC

 
package{
  import flash.display.Sprite;
  
  public class Main extends Sprite{
    public function Main(){

        var aNumbers:Array = [10, 1, 2, 15, 21, 13, 33, 3];
        aNumbers.sort();
        trace(aNumbers.toString());
        var aNumbers:Array = [10, 1, 2, 15, 21, 13, 33, 3];
        aNumbers.sort(Array.NUMERIC);
        trace(aNumbers.toString());
    }
  }
}
1,10,13,15,2,21,3,33
1,2,3,10,13,15,21,33



Sorting Regardless of Case

 
package{
  import flash.display.Sprite;
  
  public class Main extends Sprite{
    public function Main(){

        var aWords:Array = ["o", "S", "a", "C"];
        aWords.sort();
        trace(aWords.toString());  //C,S,a,o
    }
  }
}



Sorting with Custom Algorithms

 
package{
  import flash.display.Sprite;
  
  public class Main extends Sprite{
    public function Main(){
        var aWords:Array = ["o", "S", "a", "C"];
        aWords.sort(sorter);
        trace(aWords.toString());
    }
        function sorter(a:String, b:String):Number {
          if(a.toUpperCase() > b.toUpperCase()) {
            return -1;
          }
          else if(a.toUpperCase() < b.toUpperCase()) {
            return 1;
          }
          else {
            return 0;
          }
        }
  }
}
S,o,C,a



Sorting with Multiple Flags

 
package{
  import flash.display.Sprite;
  
  public class Main extends Sprite{
    public function Main(){
        var aWords:Array = ["o", "S", "a", "C"];
        aWords.sort(Array.DESCENDING | Array.CASEINSENSITIVE);
        trace(aWords.toString()); // S,o,C,a
    }
  }
}



sort( ) method runs a case-sensitive sort by default

 
package{
  import flash.display.Sprite;
  
  public class Main extends Sprite{
    public function Main(){
        var words:Array = ["T", "r", "a", "j"];
        words.sort(  );
        trace(words);  //T,a,j,r
    }
  }
}



Sort the array only if it contains unique elements: Array.UNIQUESORT

 

package{
  import flash.display.Sprite;
  
  public class Main extends Sprite{
    public function Main(){
        var ranking:Array = [2,5,6,3,1,1,4,8,7,10,9];
        var sortedRanking:Object = ranking.sort(Array.UNIQUESORT);
        trace(sortedRanking);   
        trace(ranking);  
    }
  }
}
0
2,5,6,3,1,1,4,8,7,10,9



Sort the array"s values first according to the type of value and then alphabetically

 
package{
  import flash.display.Sprite;
  
  public class Main extends Sprite{
    public function Main(){
        var aPlaces:Array = ["B", "V", "J", "C","M", "P"];
        aPlaces.sort(sorter);
        trace(aPlaces.toString());
    }
    function isInArray(sElement:String, aArray:Array) {
      for(var i:Number = 0; i < aArray.length; i++) {
        if(sElement == aArray[i]) {
          return true;
        }
      }
      return false;
    }
    function sorter(a:String, b:String):Number {
      var aCountries:Array = ["M", "V", "J"];
      var aCities:Array = ["C", "P", "B"];
      if((isInArray(a, aCountries) && isInArray(b, aCities)) ||
    (isInArray(b, aCountries) && isInArray(a, aCities))) {
        return 1;
      }
      if(a.toUpperCase() > b.toUpperCase()) {
        return 1;
      }
      else if(a.toUpperCase() < b.toUpperCase()) {
        return -1;
      }
      else {
        return 0;
      }
    }
  }
}
B,C,J,M,P,V



Use the Array.CASEINSENSITIVE constant to run a case-insensitive sort:

 
package{
  import flash.display.Sprite;
  
  public class Main extends Sprite{
    public function Main(){
        var words:Array = ["T", "r", "a", "j"];
        words.sort(Array.CASEINSENSITIVE);
        trace(words); //a,j,r,T
    }
  }
}



Use the Array.NUMERIC constant with the sort( ) method to sort an array of numbers numerically

 
package{
  import flash.display.Sprite;
  
  public class Main extends Sprite{
    public function Main(){
        var scores:Array = [10, 2, 14, 5, 8, 20, 19, 6];
        scores.sort(Array.NUMERIC);
        trace(scores);   //2,5,6,8,10,14,19,20
    }
  }
}



When you sort an array of numbers, the values are sorted according to the ASCII equivalents of the digits

 
package{
  import flash.display.Sprite;
  
  public class Main extends Sprite{
    public function Main(){
        var scores:Array = [10, 2, 14, 5, 8, 20, 19, 6];
        scores.sort(  );
        trace(scores); //10,14,19,2,20,5,6,8
    }
  }
}