Marcelo Noronha
Marcelo Noronha

Reputation: 815

Sorting Array AS3 - Part 2

I have this code:

var a:Array = [ "Ramsey", "Tusey", "Iuser","Sephora",'user', 'reseo', 'nesey', 'sela']

a.sort(sortF)

How would be sortF (sort function) to sort for words that have "s" or "S" in its index 0 (bring to front), and then with that new array disposition sort again (all the array including the words that have not 's' or "S" in its index 0) alphanumeric case insensitive, but, keeping the words that have "s" in its index 0 at front but in alphanumeric order.

Thanks.

Upvotes: 0

Views: 179

Answers (1)

HeyYO
HeyYO

Reputation: 2073

var sthis:Array = [ "Ramsey", "Tusey", "Iuser","Sephora",'user', 'reseo', 'nesey', 'sela'];

function sortfor(sa:Array,s:String):Array{
        s=s.toLowerCase();
        var firstpart:Array = new Array();
        var secondpart:Array = new Array();
        for each (var el in sa) {
            if(el.toLowerCase().indexOf(s)==0){
                firstpart.push(el);
            }else{
                secondpart.push(el);
            }
        }
        firstpart.sort();
        return firstpart.concat(secondpart);
}

trace(sortfor(sthis,"se"));

Upvotes: 1

Related Questions