TASK DESCRIPTION:
I have a Company class, holding some data of a company, such as English name, Chinese name and on which floor it is located. It has no method, so it functions exactly in the same way as struct in C/C++. And I have an array of Company objects.
I've got two missions now. The first one is to classify the Company objects into different arrays, by its one certain property, say, business nature. And the second one is to perform searching throughout the array of Company.
Mission 1: Classification
This one is pretty simple. First of all we need a loop on the array; then inside each step, check if we already record on the Company's business nature, if yes, we push it in to the array for that business nature, if not we store that business and push the Company into a brand new array.
So we need:
an array of business: _businessNatures,
a 2-dimesion array, its elements are arrays of Company, one per one business nature, the business string is stored in_businessNature array with the same index:
_byBussNature;
an index keep track of the current index of the array we are accessing: $idx_flr
package
{
......
import model.Company;
public class main extends Sprite
{
......
private var _companyList:Array;
private var _businessNatures:Array;
private var _byBussNature:Array;
public function main()
{
......
}
......
private function classifyArray():void
{
this._businessNatures = new Array();
this._byBussNature = new Array();
var $idx_bntr:int;
for(var $i:int = 0; $i < this._companyList.length; $i++)
{
$idx_bntr = this._businessNatures.indexOf(this._companyList[$i].bntr);
if( -1 == $idx_bntr )
{
this._businessNatures.push( this._companyList[$i].bntr );
var $tempArray_bntr:Array = new Array();
$tempArray_bntr.push( this._companyList[$i] );
this._byBussNature.push( $tempArray_bntr );
}
else
{
this._byBussNature[$idx_bntr].push( this._companyList[$i] );
}
}
}
......
}
}
Nothing special, but just involves the usage of indexOf method of Array, it will return the index of the value you find, otherwise return -1, to incidate that the value doesn't exist. I use this method to find if the string present business nature have already been around there.
Mission 2: Searching
This task is done with filter method of Array. You need to design your own filter function, which must return Boolean type value, and inside the body you define your own comparision criteria. And compiler will use your criteria to perform the comparision on each element of the array, then return the ones meet it.
package
{
......
import model.Company;
public class main extends Sprite
{
......
private var _companyList:Array;
private var _searchValue_Alph:String;
public function main()
{
......
}
......
private function filterArray():void
{
var $searchResult:Array = this._companyList.filter(alphaFilter);
}
private function alphaFilter($element:Company, $index:int, $arr:Array):Boolean
{
var $low:String = Company($element).e.toLowerCase();
return ( $low.indexOf( this._searchValue_Alph.toLowerCase() ) > 0 );
}
......
}
}
Highlights In the end
The Company objects are complex data type, so the two new classfied arrays hold the reference to the values stored in _companyList array, instead of new copys. Manipulation on the former will affect the values in the later. Paste the piece and run:
public function main()
{
classifyArray();
trace(this._companyList[178].c);
trace(this._byBussNature[4][10].c);
this._byBussNature[4][10].c = "中國聯通";
trace(this._companyList[178].c);
}
The output:
中國電信(香港)國際有限公司
中國電信(香港)國際有限公司
中國聯通
REFS:
http://www.onebyonedesign.com/tutorials/array_methods/
http://forums.adobe.com/message/3954482