Packageorg.as3utils
Classpublic class ArrayUtil
InheritanceArrayUtil Inheritance Object

A utility class to work with Array objects.

ArrayUtil handles null input arrays quietly in almost all methods. When not, it's documented in the method. That is to say that a null input will not thrown an error in almost all methods.



Public Methods
 MethodDefined By
  
ArrayUtil is a static class and shouldn't be instantiated.
ArrayUtil
  
addAt(array:Array, index:int, element:*):Array
[static] Adds the element at the specified position in the specified array.
ArrayUtil
  
contains(array:Array, element:*):Boolean
[static] Checks if the element is in the given array.
ArrayUtil
  
containsDuplication(array:Array):Boolean
[static]
ArrayUtil
  
containsOnlyType(array:Array, type:*, strict:Boolean = false):Boolean
[static] Returns true if the array contains only elements of the type argument.
ArrayUtil
  
containsType(array:Array, type:*, strict:Boolean = false):Boolean
[static] Returns true if the array contains some element of the type argument.
ArrayUtil
  
equals(array1:Array, array2:Array):Boolean
[static] Compares two arrays, returning true if they are equal (same length, objects and order).
ArrayUtil
  
filterByType(array:Array, type:Class, strict:Boolean = false):Array
[static] Returns the array containing only objects of the type of the type argument.
ArrayUtil
  
getDuplicate(array:Array):Array
[static]
ArrayUtil
  
isEmpty(array:Array):Boolean
[static] Checks if an array is empty (zero-length) or null.
ArrayUtil
  
isNotEmpty(array:Array):Boolean
[static] Checks if the array is not empty (zero-length) nor not null.
ArrayUtil
  
isSameLength(array1:Array, array2:Array):Boolean
[static] Checks whether two arrays are the same length, treating null arrays as length 0.
ArrayUtil
  
maxValue(array:Array):Number
[static] Returns the largest number in the array.
ArrayUtil
  
maxValueIndex(array:Array):int
[static] Returns the index position of the largest number in the array.
ArrayUtil
  
nullToEmpty(array:Array):Array
[static] Returns an empty array for a null input array.
ArrayUtil
  
removeAllOccurances(array:Array, element:*):Array
[static] Removes all occurances of a the given element argument from the given array argument.
ArrayUtil
  
removeAt(array:Array, index:int):Array
[static] Removes the element at the specified position from the specified array.
ArrayUtil
  
removeFirstOccurance(array:Array, element:*):Array
[static] Removes the first occurrence of the specified element from the specified array.
ArrayUtil
  
removeLastOccurance(array:Array, element:*):Array
[static] Removes the last occurrence of the specified element from the specified array.
ArrayUtil
  
shuffle(array:Array):Array
[static] Shuffles the position of the elements of the given array.
ArrayUtil
  
sortAlphabetically(array:Array, comparison:AlphabeticalComparison):Array
[static] Sorts the array of String objects alphabetically.
ArrayUtil
  
sortAlphabeticallyByObjectProperty(array:Array, property:String, comparison:AlphabeticalComparison):Array
[static] Sorts the array of objects alphabetically through the object's property.
ArrayUtil
  
sortAscending(array:Array):Array
[static] Sorts the array of Number objects ascending.
ArrayUtil
  
sortAscendingByObjectProperty(array:Array, property:String):Array
[static] Sorts the array of objects ascending through the object's property (must be a numeric value).
ArrayUtil
  
sortDescending(array:Array):Array
[static] Sorts the array of Number objects descending.
ArrayUtil
  
sortDescendingByObjectProperty(array:Array, property:String):Array
[static] Sorts the array of objects descending through the object's property (must be a numeric value).
ArrayUtil
  
swap(array:Array, element1:*, element2:*):void
[static] Swaps the position of the elements.
ArrayUtil
  
swapAt(array:Array, index1:*, index2:*):void
[static] Swaps the position of the elements in the specified indexes.
ArrayUtil
  
swapPositions(array:Array, newPositions:Array):void
[static] Swaps the position of all elements in the array according to newPositions.
ArrayUtil
  
uniqueCopy(array:Array):Array
[static] Creates a new array containing only unique instances of the objects in the given array.
ArrayUtil
Constructor Detail
ArrayUtil()Constructor
public function ArrayUtil()

ArrayUtil is a static class and shouldn't be instantiated.


Throws
IllegalOperationError — ArrayUtil is a static class and shouldn't be instantiated.
Method Detail
addAt()method
public static function addAt(array:Array, index:int, element:*):Array

Adds the element at the specified position in the specified array.

This method modifies the original array.

Parameters

array:Array — the array to remove the element from.
 
index:int — the position of the element to be removed.
 
element:* — the element to add.

Returns
Array — the array with the element added.

Throws
ArgumentError — if the array argument is null.
 
RangeError — if the index argument is a negative integer or greater than the array.length - 1.

Example
         import org.as3coreaddendum.utils.ArrayUtil;
         
         var arr:Array = ["abc", "def", 123, 1, 2, 3, "abc", 7];
         
         ArrayUtil.addAt(arr, 0, 0)       // [0,abc,def,123,1,2,3,abc,7]
         ArrayUtil.addAt(arr, 7, 4)       // [0,abc,def,123,1,2,3,4,abc,7]
         ArrayUtil.addAt(arr, 10, "z")    // [0,abc,def,123,1,2,3,4,abc,7,z]
         
         
contains()method 
public static function contains(array:Array, element:*):Boolean

Checks if the element is in the given array.

Parameters

array:Array — the array to search through. May be null.
 
element:* — the element to find.

Returns
Booleantrue if the array contains the element.

Example
         import org.as3coreaddendum.utils.ArrayUtil;
         
         var obj:Object = {label:"ghi"};
         var arr:Array = ["abc", 123, {label:"def"}, obj];
         
         ArrayUtil.contains([], null)              // false
         ArrayUtil.contains([null], null)          // true
         ArrayUtil.contains(arr, "abc")            // true
         ArrayUtil.contains(arr, "ab")             // false
         ArrayUtil.contains(arr, "abcd")           // false
         ArrayUtil.contains(arr, 123)              // true
         ArrayUtil.contains(arr, "123")            // false
         ArrayUtil.contains(arr, 1234)             // false
         ArrayUtil.contains(arr, {label:"def"})    // false
         ArrayUtil.contains(arr, {label:"ghi"})    // false
         ArrayUtil.contains(arr, obj)              // true
         
containsDuplication()method 
public static function containsDuplication(array:Array):Boolean

Parameters

array:Array

Returns
Boolean
containsOnlyType()method 
public static function containsOnlyType(array:Array, type:*, strict:Boolean = false):Boolean

Returns true if the array contains only elements of the type argument.

Parameters

array:Array — the array to check. May be null.
 
type:* — the type of the elements.
 
strict:Boolean (default = false) — defines if the type of the elements should be strictly equal.

Returns
Booleantrue if the array contains only elements of the type argument. If the array is null or empty returns false.

Example
         import org.as3coreaddendum.utils.ArrayUtil;
         
         ArrayUtil.containsOnlyType(["a", "b", "c"], String)                  // true
         ArrayUtil.containsOnlyType(["a", "b", "c"], String, true)            // true
         ArrayUtil.containsOnlyType(["a", "b", "c", 5], String)               // false
         ArrayUtil.containsOnlyType([[], [], [], [], []], Array)              // true
         ArrayUtil.containsOnlyType([[], [], [], 3, [], []], Array)           // false
         ArrayUtil.containsOnlyType([[], [], [], 3, [], []], Object)          // true
         ArrayUtil.containsOnlyType([[], [], [], 3, [], []], Object, true)    // false
         
containsType()method 
public static function containsType(array:Array, type:*, strict:Boolean = false):Boolean

Returns true if the array contains some element of the type argument.

Parameters

array:Array — the array to check. May be null.
 
type:* — the type of the elements.
 
strict:Boolean (default = false) — defines if the type of the elements should be strictly equal.

Returns
Booleantrue if the array contains some element of the type argument. If the array is null or empty returns false.

Example
         import org.as3coreaddendum.utils.ArrayUtil;
         
         ArrayUtil.containsType(["a", "b", "c"], String)                  // true
         ArrayUtil.containsType(["a", "b", "c"], String, true)            // true
         ArrayUtil.containsType(["a", "b", "c", 5], String)               // true
         ArrayUtil.containsType(["a", "b", "c"], Number):                 // false
         ArrayUtil.containsType(["a", "b", "c", 5], Number)               // true
         ArrayUtil.containsType([[], [], [], [], []], Array)              // true
         ArrayUtil.containsType([[], [], [], 3, [], []], Array)           // true
         ArrayUtil.containsType([[], [], [], 3, [], []], Number)          // true
         ArrayUtil.containsType([[], [], [], 3, [], []], Object)          // true
         ArrayUtil.containsType([[], [], [], 3, [], []], Object, true)    // false
         ArrayUtil.containsType([[], [], [], 3, [], []], Boolean)         // false
         
equals()method 
public static function equals(array1:Array, array2:Array):Boolean

Compares two arrays, returning true if they are equal (same length, objects and order).

Two null references are considered to be equal.

Parameters

array1:Array — the first array. May be null.
 
array2:Array — the second array. May be null.

Returns
Booleantrue if the arrays are equal or both are null.

Example
         import org.as3coreaddendum.utils.ArrayUtil;
         
         var obj:Object = {label:"jlm"};
         var n1:int = 1;
         var n2:Number = 2;
         var n3:Number = 3.1;
         var arr:Array = ["abc", "def", 123, {label:"ghi"}, obj, n1, n2, n3];
         var arr2:Array = ["abc", "def", 123, {label:"ghi"}, obj, n1, n2, n3];
         var arr3:Array = ["abc", "def", 123, obj, n1, n2, n3];
         var arr4:Array = ["abc", "def", 123, obj, n1, n2, n3];
         var arr5:Array = ["abc", "def", 123, obj, n2, n1, n3];
         
         ArrayUtil.equals(null, null)           // true
         ArrayUtil.equals(null, [])             // false
         ArrayUtil.equals([], [])               // true
         ArrayUtil.equals([1], [1])             // true
         ArrayUtil.equals(["abc"], ["abc"])     // true
         ArrayUtil.equals(["abc"], ["abcd"])    // false
         ArrayUtil.equals(arr, [])              // false
         ArrayUtil.equals(arr, arr)             // true
         ArrayUtil.equals(arr, arr2)            // false
         ArrayUtil.equals(arr3, arr4)           // true
         ArrayUtil.equals(arr4, arr5)           // false
         
filterByType()method 
public static function filterByType(array:Array, type:Class, strict:Boolean = false):Array

Returns the array containing only objects of the type of the type argument.

This method modifies the original array.

Parameters

array:Array — the array for filtering. May be null.
 
type:Class — the type of the objects that should remain in the array.
 
strict:Boolean (default = false) — defines if the type of the elements should be strictly equal.

Returns
Array — the array containing only objects of the type of the type argument.

Example
         import org.as3coreaddendum.utils.ArrayUtil;
         
         var obj:Object = {label:"jlm"};
         var n1:int = 1;
         var n2:Number = 2;
         var n3:Number = 3.1;
         var arr:Array = ["abc", "def", 123, {label:"ghi"}, obj, n1, n2, n3];
         
         ArrayUtil.filterByType(null, null)             // null
         ArrayUtil.filterByType([], null)               // 
         ArrayUtil.filterByType(null, Array)            // null
         ArrayUtil.filterByType(arr, String)            // abc,def
         ArrayUtil.filterByType(arr, String, true)      // abc,def
         ArrayUtil.filterByType(arr, Object)            // abc,def,123,[object Object],[object Object],1,2,3.1
         ArrayUtil.filterByType(arr, Object, true)      // [object Object],[object Object]
         ArrayUtil.filterByType(arr, Number)            // 123,1,2,3.1
         ArrayUtil.filterByType(arr, Number, true)      // 3.1
         ArrayUtil.filterByType(arr, int)               // 123,1,2
         ArrayUtil.filterByType(arr, int, true)         // 123,1,2
         
         // for this to work the array must be re-declared with values for each call, because filterByType changes the original array.
         
getDuplicate()method 
public static function getDuplicate(array:Array):Array

Parameters

array:Array

Returns
Array
isEmpty()method 
public static function isEmpty(array:Array):Boolean

Checks if an array is empty (zero-length) or null.

Parameters

array:Array — the array to check.

Returns
Booleantrue if the array is empty or null.

Example
         import org.as3coreaddendum.utils.ArrayUtil;
         
         ArrayUtil.isEmpty(null)      // true
         ArrayUtil.isEmpty([])        // true
         ArrayUtil.isEmpty([null])    // false
         ArrayUtil.isEmpty([123])     // false
         
isNotEmpty()method 
public static function isNotEmpty(array:Array):Boolean

Checks if the array is not empty (zero-length) nor not null.

Parameters

array:Array — the array to check.

Returns
Booleantrue if the array is not empty nor not null.

Example
         import org.as3coreaddendum.utils.ArrayUtil;
         
         ArrayUtil.isNotEmpty(null)      // false
         ArrayUtil.isNotEmpty([])        // false
         ArrayUtil.isNotEmpty([null])    // true
         ArrayUtil.isNotEmpty([123])     // true
         
isSameLength()method 
public static function isSameLength(array1:Array, array2:Array):Boolean

Checks whether two arrays are the same length, treating null arrays as length 0.

Two null references are considered to be equal.

Parameters

array1:Array — the first array. May be null.
 
array2:Array — the second array. May be null.

Returns
Booleantrue if length of arrays matches, treating null as an empty array.

Example
         import org.as3coreaddendum.utils.ArrayUtil;
         
         ArrayUtil.isSameLength(null, null)            // true
         ArrayUtil.isSameLength(null, [])              // true
         ArrayUtil.isSameLength([], [])                // true
         ArrayUtil.isSameLength([1], [2])              // true
         ArrayUtil.isSameLength([1, "a"], [2])         // false
         ArrayUtil.isSameLength([1, "a"], [2, "b"])    // true
         
maxValue()method 
public static function maxValue(array:Array):Number

Returns the largest number in the array.

Parameters

array:Array — the array to check. May be null.

Returns
Number — the largest number in the array. If the array argument is null or empty then the return is NaN.

Example
         import org.as3coreaddendum.utils.ArrayUtil;
         
         var arr:Array = [5, 3, 9.1, 9.12, -10];
         var arr2:Array = [-15, -8, -25, -90, -66];
         
         ArrayUtil.maxValue(null)    // NaN
         ArrayUtil.maxValue([])      // NaN
         ArrayUtil.maxValue(arr)     // 9.12
         ArrayUtil.maxValue(arr2)    // -8
         
maxValueIndex()method 
public static function maxValueIndex(array:Array):int

Returns the index position of the largest number in the array.

Parameters

array:Array — the array to check. May be null.

Returns
int — the index position of the largest number in the array. If the array argument is null or empty then the return is -1.

Example
         import org.as3coreaddendum.utils.ArrayUtil;
         
         var arr:Array = [5, 3, 9.1, 9.12, -10];
         var arr2:Array = [-15, -8, -25, -90, -66];
         
         ArrayUtil.maxValue(null)    // -1
         ArrayUtil.maxValue([])      // -1
         ArrayUtil.maxValue(arr)     // 3
         ArrayUtil.maxValue(arr2)    // 1
         
nullToEmpty()method 
public static function nullToEmpty(array:Array):Array

Returns an empty array for a null input array.

Parameters

array:Array — the array to check for null or empty.

Returns
Array — the same array or an empty array if null input.

Example
         import org.as3coreaddendum.utils.ArrayUtil;
         
         ArrayUtil.nullToEmpty(null)            // []
         ArrayUtil.nullToEmpty([])              // []
         ArrayUtil.nullToEmpty([1])             // [1]
         ArrayUtil.nullToEmpty(["abc", 123])    // [abc,123]
         
removeAllOccurances()method 
public static function removeAllOccurances(array:Array, element:*):Array

Removes all occurances of a the given element argument from the given array argument.

This method modifies the original array.

Parameters

array:Array — the array to remove the element. May be null.
 
element:* — the element to be removed from the array.

Returns
Array — the updated array or null if the array argument is null.

Example
         import org.as3coreaddendum.utils.ArrayUtil;
         
         var arr:Array = ["abc", "def", 123, 1, 2, 3, "abc", 7];
         
         ArrayUtil.removeAllOccurances(arr, "abc")    // [def,123,1,2,3,7]
         
removeAt()method 
public static function removeAt(array:Array, index:int):Array

Removes the element at the specified position from the specified array.

This method modifies the original array.

Parameters

array:Array — the array to remove the element from.
 
index:int — the position of the element to be removed.

Returns
Array — the array with the element removed.

Throws
ArgumentError — if the array argument is null or empty.
 
RangeError — if the index argument is a negative integer or greater than the array.length - 1.

Example
         import org.as3coreaddendum.utils.ArrayUtil;
         
         var arr:Array = ["abc", "def", 123, 1, 2, 3, "abc", 7];
         
         ArrayUtil.removeAt(arr, 0)    // [def,123,1,2,3,abc,7]
         ArrayUtil.removeAt(arr, 5)    // [abc,def,123,1,2,abc,7]
         ArrayUtil.removeAt(arr, 6)    // [abc,def,123,1,2,3,7]
         
         //for this to work exactly that way is necessary to redefine the array for every call because the original array is modified at each call.
         
removeFirstOccurance()method 
public static function removeFirstOccurance(array:Array, element:*):Array

Removes the first occurrence of the specified element from the specified array.

This method modifies the original array.

Parameters

array:Array — the array to remove the element from.
 
element:* — the element to be removed.

Returns
Array — the array with the element removed.

Throws
ArgumentError — if the array argument is null or empty.

Example
         import org.as3coreaddendum.utils.ArrayUtil;
         
         var arr:Array = ["abc", "def", 123, 1, 2, 3, "abc", 7];
         
         ArrayUtil.removeFirstOccurance(arr, "abc")    // [def,123,1,2,3,abc,7]
         
removeLastOccurance()method 
public static function removeLastOccurance(array:Array, element:*):Array

Removes the last occurrence of the specified element from the specified array.

This method modifies the original array.

Parameters

array:Array — the array to remove the element from.
 
element:* — the element to be removed.

Returns
Array — the array with the element removed.

Throws
ArgumentError — if the array argument is null or empty.

Example
         import org.as3coreaddendum.utils.ArrayUtil;
         
         var arr:Array = ["abc", "def", 123, 1, 2, 3, "abc", 7];
         
         ArrayUtil.removeLastOccurance(arr, "abc")    // [abc,def,123,1,2,3,7]
         
shuffle()method 
public static function shuffle(array:Array):Array

Shuffles the position of the elements of the given array.

This method modifies the original array.

Parameters

array:Array — the array to shuffle. May be null.

Returns
Array — the modified array.

Example
         import org.as3coreaddendum.utils.ArrayUtil;
         
         var arr:Array = ["abc", "def", 123, 1, 2, 3, "abc", 7];
         
         ArrayUtil.shuffle(arr)    // [123,abc,2,def,abc,7,3,1]
         
sortAlphabetically()method 
public static function sortAlphabetically(array:Array, comparison:AlphabeticalComparison):Array

Sorts the array of String objects alphabetically.

This method uses the org.as3coreaddendum.system.comparators.AlphabeticalComparator object.

This method modifies the original array.

Parameters

array:Array — the array to sort.
 
comparison:AlphabeticalComparison — indicates which type of comparison will be used.

Returns
Array — the sorted array.

See also

AlphabeticalComparator

Example
         import org.as3coreaddendum.utils.ArrayUtil;
         
         var arr:Array = ["a", "ab", "abc", "aba", "A", "Ab", "Abc", "Aba", "AB", "ABC", "ABA"];
         
         ArrayUtil.shuffle(arr)                                                          // [aba,Ab,ABC,AB,abc,Aba,ab,ABA,a,Abc,A]
         
         ArrayUtil.sortAlphabetically(arr, AlphabeticalComparison.LOWER_CASE_FIRST)      // [a,ab,aba,abc,A,Ab,Aba,Abc,AB,ABA,ABC]
         ArrayUtil.sortAlphabetically(arr, AlphabeticalComparison.UPPER_CASE_FIRST)      // [A,AB,ABA,ABC,Ab,Aba,Abc,a,ab,aba,abc]
         ArrayUtil.sortAlphabetically(arr, AlphabeticalComparison.CASE_INSENSITIVE)      // [a,A,AB,ab,Ab,ABA,Aba,aba,ABC,Abc,abc]
         
sortAlphabeticallyByObjectProperty()method 
public static function sortAlphabeticallyByObjectProperty(array:Array, property:String, comparison:AlphabeticalComparison):Array

Sorts the array of objects alphabetically through the object's property.

This method uses the org.as3coreaddendum.system.comparators.AlphabeticalComparator and org.as3coreaddendum.system.comparators.PropertyComparator objects together.

This method modifies the original array.

Parameters

array:Array — the array to sort.
 
property:String — the name of the property to be recovered and compared between the objects.
 
comparison:AlphabeticalComparison — indicates which type of comparison will be used.

Returns
Array — the sorted array.

See also

AlphabeticalComparator
PropertyComparator

Example
         import org.as3coreaddendum.utils.ArrayUtil;
         
         var arr:Array = [{label:"a"}, {label:"ab"}, {label:"abc"}, {label:"aba"}, {label:"A"}, {label:"Ab"}, {label:"Abc"}, {label:"Aba"}, {label:"AB"}, {label:"ABC"}, {label:"ABA"}];
         
         ArrayUtil.shuffle(arr)
         ArrayUtil.sortAlphabeticallyByObjectProperty(arr, "label", AlphabeticalComparison.CASE_INSENSITIVE)
         
         arr[0].label    // A
         arr[1].label    // a
         arr[2].label    // Ab
         arr[3].label    // ab
         arr[4].label    // AB
         arr[5].label    // Aba
         arr[6].label    // ABA
         arr[7].label    // aba
         arr[8].label    // ABC
         arr[9].label    // abc
         
sortAscending()method 
public static function sortAscending(array:Array):Array

Sorts the array of Number objects ascending.

This method modifies the original array.

Parameters

array:Array — the array to sort.

Returns
Array — the sorted array.

Example
         import org.as3coreaddendum.utils.ArrayUtil;
         
         var arr:Array = [-3, -2.55, -2.54, -1, 0, 1, 2, 3, 4, 4.1, 5, 5.01, 5.02, 6, 7, 8];
         
         ArrayUtil.shuffle(arr)          // [-1,4.1,6,3,-3,4,5.02,8,2,1,5,-2.54,5.01,7,-2.55,0]
         
         ArrayUtil.sortAscending(arr)    // [-3,-2.55,-2.54,-1,0,1,2,3,4,4.1,5,5.01,5.02,6,7,8]
         
sortAscendingByObjectProperty()method 
public static function sortAscendingByObjectProperty(array:Array, property:String):Array

Sorts the array of objects ascending through the object's property (must be a numeric value).

This method modifies the original array.

Parameters

array:Array — the array to sort.
 
property:String — the name of the property to be recovered and compared between the objects.

Returns
Array — the sorted array.

Example
         import org.as3coreaddendum.utils.ArrayUtil;
         
         var arr:Array = [{num:-3}, {num:-2.55}, {num:-2.54}, {num:-1}, {num:0}, {num:1}, {num:2}, {num:3}, {num:4}, {num:4.1}, {num:5}, {num:5.01}, {num:5.02}, {num:6}, {num:7}, {num:8}];
         
         ArrayUtil.shuffle(arr)             // [-1,4.1,6,3,-3,4,5.02,8,2,1,5,-2.54,5.01,7,-2.55,0]
         
         ArrayUtil.sortAscendingByObjectProperty(arr, "num")
         
         arr[0].num     // -3
         arr[1].num     // -2.55
         arr[2].num     // -2.54
         arr[3].num     // -1
         arr[4].num     // 0
         arr[5].num     // 1
         arr[6].num     // 2
         arr[7].num     // 3
         arr[8].num     // 4
         arr[9].num     // 4.1
         arr[10].num    // 5
         arr[11].num    // 5.01
         arr[12].num    // 5.02
         arr[13].num    // 6
         arr[14].num    // 7
         arr[15].num    // 8
         
sortDescending()method 
public static function sortDescending(array:Array):Array

Sorts the array of Number objects descending.

This method modifies the original array.

Parameters

array:Array — the array to sort.

Returns
Array — the sorted array.

Example
         import org.as3coreaddendum.utils.ArrayUtil;
         
         var arr:Array = [-3, -2.55, -2.54, -1, 0, 1, 2, 3, 4, 4.1, 5, 5.01, 5.02, 6, 7, 8];
         
         ArrayUtil.shuffle(arr)           // [-1,4.1,6,3,-3,4,5.02,8,2,1,5,-2.54,5.01,7,-2.55,0]
         
         ArrayUtil.sortDescending(arr)    // [8,7,6,5.02,5.01,5,4.1,4,3,2,1,0,-1,-2.54,-2.55,-3]
         
sortDescendingByObjectProperty()method 
public static function sortDescendingByObjectProperty(array:Array, property:String):Array

Sorts the array of objects descending through the object's property (must be a numeric value).

This method modifies the original array.

Parameters

array:Array — the array to sort.
 
property:String — the name of the property to be recovered and compared between the objects.

Returns
Array — the sorted array.

Example
         import org.as3coreaddendum.utils.ArrayUtil;
         
         var arr:Array = [{num:-3}, {num:-2.55}, {num:-2.54}, {num:-1}, {num:0}, {num:1}, {num:2}, {num:3}, {num:4}, {num:4.1}, {num:5}, {num:5.01}, {num:5.02}, {num:6}, {num:7}, {num:8}];
         
         ArrayUtil.shuffle(arr)             // [-1,4.1,6,3,-3,4,5.02,8,2,1,5,-2.54,5.01,7,-2.55,0]
         
         ArrayUtil.sortDescendingByObjectProperty(arr, "num")
         
         arr[0].num     // 8
         arr[1].num     // 7
         arr[2].num     // 6
         arr[3].num     // 5.02
         arr[4].num     // 5.01
         arr[5].num     // 5
         arr[6].num     // 4.1
         arr[7].num     // 4
         arr[8].num     // 3
         arr[9].num     // 2
         arr[10].num    // 1
         arr[11].num    // 0
         arr[12].num    // -1
         arr[13].num    // -2.54
         arr[14].num    // -2.55
         arr[15].num    // -3
         
swap()method 
public static function swap(array:Array, element1:*, element2:*):void

Swaps the position of the elements.

This method modifies the original array.

Parameters

array:Array — the array to swap the position of the elements.
 
element1:* — the first element.
 
element2:* — the second element.


Example
         import org.as3coreaddendum.utils.ArrayUtil;
         
         var arr:Array = [1, 9, 3, 7, 5];
         
         ArrayUtil.swap(arr, 9, 5)      // [1,5,3,7,9]
         
         ArrayUtil.swap(arr, 3, 5)      // [1,3,5,7,9]
         
swapAt()method 
public static function swapAt(array:Array, index1:*, index2:*):void

Swaps the position of the elements in the specified indexes.

This method modifies the original array.

Parameters

array:Array — the array to swap the position of the elements.
 
index1:* — the index of the first element.
 
index2:* — the index of the second element.


Example
         import org.as3coreaddendum.utils.ArrayUtil;
         
         var arr:Array = [1, 9, 3, 7, 5];
         
         ArrayUtil.swapAt(arr, 1, 4)      // [1,5,3,7,9]
         
         ArrayUtil.swapAt(arr, 2, 1)      // [1,3,5,7,9]
         
swapPositions()method 
public static function swapPositions(array:Array, newPositions:Array):void

Swaps the position of all elements in the array according to newPositions.

The length of both arrays must be equal.

This method modifies the original array.

Parameters

array:Array — the array to swap the position of all elements.
 
newPositions:Array — the array with the new positions.


Example
         import org.as3coreaddendum.utils.ArrayUtil;
         
         var arr:Array = ["c", "a", "b", "e", "d"];
         
         var positions:Array = [1, 2, 0, 4, 3];
         
         ArrayUtil.swapPositions(arr, positions)     // [a,b,c,d,e]
         
uniqueCopy()method 
public static function uniqueCopy(array:Array):Array

Creates a new array containing only unique instances of the objects in the given array. In other words, removes duplicated objects.

Parameters

array:Array — the array to copy.

Returns
Array — a new array without duplicated objects.

Example
         import org.as3coreaddendum.utils.ArrayUtil;
         
         var arr:Array = ["a", "b", "c", "a", "b", "a"];
         
         ArrayUtil.uniqueCopy(arr)    // [a,b,c]