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