Sort the array's values first according to the type of value and then alphabetically : Sort « Array « Flash / Flex / ActionScript






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

        








Related examples in the same category

1.Sorting Arrays
2.Sorting Numerically: Array.NUMERIC
3.Sorting in Descending Order
4.Sorting Regardless of Case
5.A case-insensitive sort using the Array.CASEINSENSITIVE constant
6.Sorting and Testing for Unique Values
7.Getting Sorted Indices
8.Sorting with Multiple Flags
9.Sorting with Custom Algorithms
10.Sort an array in descending order using the Array.DESCENDING constant:
11.sort( ) method runs a case-sensitive sort by default
12.Use the Array.CASEINSENSITIVE constant to run a case-insensitive sort:
13.When you sort an array of numbers, the values are sorted according to the ASCII equivalents of the digits
14.Use the Array.NUMERIC constant with the sort( ) method to sort an array of numbers numerically
15.Sort the array only if it contains unique elements: Array.UNIQUESORT
16.Get the sorted order of an array's elements without changing the original array: Array.RETURNINDEXEDARRAY
17.Combine the combine the constants using the bitwise OR operator (|)
18.Reverse the order of the elements in an array
19.Call the sort( ) method passing the bandNameSort compare function:
20.Randomizing the Elements of an Array
21.Getting the Minimum or Maximum Element