Sorting methods for array arrays in AS3 (sort, Sorton)

Source: Internet
Author: User
Tags arrays bitwise numeric sorts zip


1,sort method



Syntax: function sort (... args): Array
Sorts the elements in an array. Flash is sorted according to Unicode values. (ASCII is a subset of Unicode.) )



By default, Array.Sort () is sorted by the instructions in the following list:
Sorting is case-sensitive (Z takes precedence over a).
Sort in ascending order (a takes precedence over B).
Modify the array to reflect the sort order, in which multiple elements with the same sort field are not continuously placed in any particular order in the sorted array.
Numeric fields are sorted by string, so 100 takes precedence over 99 because the string value of "1" is lower than "9".
If you want to sort by using a setting that differs from the default settings, you can use one of the sort options described in the entry for the options parameter, or you can create your own custom functions to sort. If you create a custom function, you can use it by calling the sort () method, using the name of your custom function as the first argument (comparefunction).



Parameters:



... args--a comparison function to determine the sort order of elements in an array. The result of a given element A and b,comparefunction can have one of the following three values:
-1, if A should appear before B in the sorted sequence
0, if A equals B
1, if A should appear after B in the sorted sequence
Options:number [optional]-one or more digits or names of the defined constants, separated by the | (bitwise OR) operator, change the default behavior of the sort to another behavior. The options parameter can accept the following values:



Array.caseinsensitive or 1
Array.descending or 2
Array.uniquesort or 4
Array.returnindexedarray or 8
Array.numeric or 16



For more information about this parameter, see the Array.sorton () method.



Note: Array.Sort () is defined in ECMA-262, but the array sorting option introduced in Flash Player 7 is a flash-specific extension of the ECMA-262 specification.




Example 1:


var vegetables:array = new Array ("Spinach",

"green pepper",

"cilantro",

"onion",

"avocado" );

Trace (vegetables); Spinach,green Pepper,cilantro,onion,avocado

vegetables.sort ();

Trace (vegetables); Avocado,cilantro,green Pepper,onion,spinach





Example 2:


var vegetables:array = new Array ("Spinach",

"green pepper",

"cilantro",

"Onion",

"avocado" );

Vegetables.sort ();

Trace (vegetables); Avocado,cilantro,onion,green Pepper,spinach

Vegetables.sort (array.caseinsensitive);

Trace (vegetables); Avocado,cilantro,green Pepper,onion,spinach





Example 3:


Var vegetables:array = new array ();

Vegetables.push (new vegetable ("lettuce",  1.49));

Vegetables.push (new vegetable ("spinach",  1.89));

Vegetables.push (new vegetable ("asparagus",  3.99));

Vegetables.push (new vegetable ("celery",  1.29));

Vegetables.push (new vegetable ("squash",  1.44));

Trace (vegetables);

 lettuce:1.49, spinach:1.89, asparagus:3.99, celery:1.29, squash:1.44

Vegetables.sort ();

Trace (vegetables);  asparagus:3.99, celery:1.29, lettuce:1.49, spinach:1.89, squash:1.44   Class

 vegetable {    private var name:String;

    private var price:Number;     public function vegetable (name:string, price:number)  {  

      this.name = name;         this.price = price;    &NBSP}     public function tostring (): String {         return  " "  + name +  ":"  + 

Price    &NBSP}}





Example 4:


Var vegetables:array = new array ();

Vegetables.push (new vegetable ("lettuce",  1.49));

Vegetables.push (new vegetable ("spinach",  1.89));

Vegetables.push (new vegetable ("asparagus",  3.99));

Vegetables.push (new vegetable ("celery",  1.29));

Vegetables.push (new vegetable ("squash",  1.44));

Trace (vegetables);

 lettuce:1.49, spinach:1.89, asparagus:3.99, celery:1.29, squash:1.44

Vegetables.sort (Sortonprice);

Trace (vegetables);  celery:1.29, squash:1.44, lettuce:1.49, spinach:1.89, asparagus:3.99   function  sortonprice (a:vegetable, b:vegetable): number {    var aprice:number

 = a.getprice ();

    var bprice:number = b.getprice ();     if (aprice > bprice)  {       

 return 1;     }  else if (aprice < bprice)  {        return

 -1;     } else  {        //aPrice 

== bprice         return 0; &NBSP;&NBSP;&NBSP;&NBSP}}   class vegetable {    private var name:

String;

    private var price:Number;     public function vegetable (name:string, price:number)  {  

      this.name = name;

        this.price = price; &NBSP;&NBSP;&NBSP;&NBSP}     public function getprice (): Number { 

       return price;     }     public fuNction tostring (): string {        return  " "  

+ name +  ":"  + price; &NBSP;&NBSP;&NBSP;&NBSP}}





2,sorton method






Syntax: function Sorton (fieldname:object, options:object = null): Array



Sorts the elements in an array based on one or more fields in a group. The array should have the following attributes:
The array is an indexed array, not an associative array.
Each element of the array contains an object that has one or more attributes.
All of these objects have at least one common property, and the value of the property can be used to sort the array. Such attributes are called field.
If you pass multiple fieldName parameters, the first field represents the primary sort field, the second field represents the next sort field, and so on. Flash is sorted according to Unicode values. (ASCII is a subset of Unicode.) If any of the two elements that are compared do not contain the fields specified in the FieldName argument, the field is considered undefined and the elements are not continuously placed in any particular order in the sorted array.



By default, Array.sorton () is sorted in the following ways:
Sorting is case-sensitive (Z takes precedence over a).
Sort in ascending order (a takes precedence over B).
Modify the array to reflect the sort order, in which multiple elements with the same sort field are not continuously placed in any particular order in the sorted array.
Numeric fields are sorted by string, so 100 takes precedence over 99 because the string value of "1" is lower than "9".



Flash Player 7 Adds the options parameter, which you can use to override the default sort behavior. To sort a simple array (for example, an array with only one field), or to specify a sort order that is not supported by the options parameter, use Array.Sort ().



To pass multiple flags, separate them by using the bitwise OR (|) Operator:
My_array.sorton (Somefieldname, array.descending | Array.numeric);



Flash Player 8 Adds the ability to specify different sorting options for each field when sorted by more than one field. In Flash Player 8, the options parameter accepts a set of sorting options so that each sort option corresponds to a sort field in the FieldName parameter. The following example uses a descending sort to sort the primary sort field a, sorts the second sorted field with a numeric sort, and sorts the third sorted field C with a case-insensitive sort:
Array.sorton (["A", "B", "C"], [Array.descending, Array.numeric, array.caseinsensitive]);



Note: The FieldName and options array must have the same number of elements, otherwise the options array will be ignored. In addition, the Array.uniquesort and Array.returnindexedarray options can only be used as the first element in the array, otherwise they are ignored.



Parameters:



fieldname:object--A string that identifies the field to be used as the sort value, or an array in which the first element represents the primary sort field, the second element represents the second sort field, and so on.
Options:object = one or more digits or names of the constants defined by null--, separated by the bitwise OR (|) operator, which can change the sort behavior. The options parameter can accept the following values:
Array.caseinsensitive or 1
Array.descending or 2
Array.uniquesort or 4
Array.returnindexedarray or 8
Array.numeric or 16



Enable code hints if you use the string form of a flag (for example, descending) instead of a numeric form (2)
Back: Array






Example 1:


Var vegetables:array = new array ();

Vegetables.push (new vegetable ("lettuce",  1.49));

Vegetables.push (new vegetable ("spinach",  1.89));

Vegetables.push (new vegetable ("asparagus",  3.99));

Vegetables.push (new vegetable ("celery",  1.29));

Vegetables.push (new vegetable ("squash",  1.44));

Trace (vegetables);

 lettuce:1.49, spinach:1.89, asparagus:3.99, celery:1.29, squash:1.44

Vegetables.sorton ("name");

Trace (vegetables);

 asparagus:3.99, celery:1.29, lettuce:1.49, spinach:1.89, squash:1.44

Vegetables.sorton ("Price",  array.numeric | array.descending);

Trace (vegetables);  asparagus:3.99, spinach:1.89, lettuce:1.49, squash:1.44, celery:1.29   Class

 vegetable {    public var name:String;

    public var price:Number;     pubLic function vegetable (name:string, price:number)  {      

  this.name = name;

        this.price = price; &NBSP;&NBSP;&NBSP;&NBSP}     public function tostring (): String {         return  " "  + name +  ":"  + 

Price &NBSP;&NBSP;&NBSP;&NBSP}}


Example 2:


Var records:array = new array ();

Records.push ({name: "John",  city: "Omaha",  zip:68144});

Records.push ({name: "John",  city: "Kansas city",  zip:72345});

Records.push ({name: "Bob",  city: "Omaha",  zip:94010}); for (var i:uint = 0; i < records.length; i++)  {   

 trace (records[i].name +  ", "  + records[i].city); }// results:// john, omaha// john, kansas city// bob, omaha Trace ("

Records.sorton (' name ',  ' city '); ");

Records.sorton (["Name",  "City"]); for (var i:uint = 0; i < records.length; i++)  {   

 trace (records[i].name +  ", "  + records[i].city); }// results:// bob, omaha// john, kansas city// john, omaha Trace ("

Records.sorton (' City ',  ' name '); "); Records. Sorton (["City",  "name"]); for (var i:uint = 0; i < records.length; i++)  {   

 trace (records[i].name +  ", "  + records[i].city); }// results:// john, kansas city// bob, omaha// john, omaha



Example 3:


class User {
    public var name:String;
    public var age:Number;
    public function User(name:String, age:uint) {
        this.name = name;
        this.age = age;
    }
    public function toString():String {
        return this.name + ":" + this.age;
    }
}
var users:Array = new Array();
users.push(new User("Bob", 3));
users.push(new User("barb", 35));
users.push(new User("abcd", 3));
users.push(new User("catchy", 4));
trace(users); // Bob:3,barb:35,abcd:3,catchy:4
users.sortOn("name");
trace(users); // Bob:3,abcd:3,barb:35,catchy:4
users.sortOn("name", Array.CASEINSENSITIVE);
trace(users); // abcd:3,barb:35,Bob:3,catchy:4
users.sortOn("name", Array.CASEINSENSITIVE | Array.DESCENDING);
trace(users); // catchy:4,Bob:3,barb:35,abcd:3
users.sortOn("age");
trace(users); // abcd:3,Bob:3,barb:35,catchy:4
users.sortOn("age", Array.NUMERIC);
trace(users); // Bob:3,abcd:3,catchy:4,barb:35
users.sortOn("age", Array.DESCENDING | Array.NUMERIC);
trace(users); // barb:35,catchy:4,Bob:3,abcd:3
var indices:Array = users.sortOn("age", Array.NUMERIC | Array.RETURNINDEXEDARRAY);
var index:uint;
for(var i:uint = 0; i < indices.length; i++) {
    index = indices[i];
    trace(users[index].name, ": " + users[index].age);
}
// Results:
// Bob : 3
// abcd : 3
// catchy : 4
// barb : 35


 




Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.