Easily sort an Array of Strings: Longest to Shortest Word

So let’s say you are making some sort of a game or program in Actionscipt 3 that requires you to sort words in order from longest to shortest. for example:

var word:Array = new Array("this", "is", "a", "list", "of", "randomly","chosen","words", "to", "sort");

if you try something like:

trace(word.sort());

you will get the following:

a,chosen,is,list,of,randomly,sort,this,to,words

That’s not what we wanted! A cool little thing that you can do in as3 is write what’s called a ‘compare function’. As I love to do, I have made a simple class with a static function that you call:

package com.frigidfish{

    public class Sort {

        public function Sort() {
            // constructor code
        }

        public static function hiToLow(a:String, b:String):Number {


            if (a.length < b.length) {
                return 1;
            }
            else if (a.length > b.length) {
                return -1;
            }
            else {
                return 0;
            }
        }
    }
}

Now just save the class under your project in the correct folder (ie c:\myproject\com\frigidfish\Sort.as).

then, you can easily sort your array like so:

import com.frigidfish.Sort;

var word:Array = new Array("this", "is", "a", "list", "of", "randomly","chosen","words", "to", "sort");

trace(word.sort(Sort.hiToLow));

The output will now be:

randomly,chosen,words,list,this,sort,of,is,to,a

Yay!

You can use compare functions to tell the built in sort function how you would specifically like your data sorted.

The function takes 2 variables that are passed by the sort function (you don’t pass anything manually to the function. it’s automatic).

Because our array contains nothing but strings, we made up 2 variable names, ‘a’ and ‘b’; both of type string.

the function can return -1, 0 or 1. Depending on the return value, the sort will be affected. Experiment with different sorting methods and see what you can come up with..

For example, to sort low to high (short word to long word in our case) you would just add this function to the Sort class:

public static function lowToHigh(a:String, b:String):Number {


            if (a.length > b.length) {
                return 1;
            }
            else if (a.length < b.length) {
                return -1;
            }
            else {
                return 0;
            }
        }

of course you will also need to call the proper function now when you sort:

trace(word.sort(Sort.lowToHigh));

I hope this helps. Just had to use it in a project myself!

Flash Actionscript 3: Easily Jumble elements of an Array or String

Sometimes in Actionscript you need an easy way to jumble elements of an array or string, such as for a card/word game. I created this simple little as3 class that you can use for just that:

// Usage:
// yourNewString = Jumble.string(yourOldString);
// yourNewArray = Jumble.array(yourOldArray);

package com.frigidfish{

    public class Jumble {

        public function Jumble() {}

        public static function string(s:String):String {
            var a:Array = s.split("");
            var r:String = "";
            a = a.sort(randomize);
            for(var i=0;i<a.length;i++){
                r += a[i];
            }          
            return r;
        }

        public static function array(a:Array):Array {
            a = a.sort(randomize);
            return a;
        }

        private static function randomize(objA:Object, objB:Object):int {
            return Math.round(Math.random()*2)-1;
        }
    }
}

Usage:

import com.frigidfish.Jumble;

var string:String = "abcdefghijklmnopqrstuvwxyz";
var array:Array = ["a","b","c","d","e","f","g","e"];

trace(Jumble.string(string));
trace(Jumble.array(array));

/*
possible output:
zvepdtnhijylarowksqfuxgcbm
e,c,e,d,a,f,g,b
*/

This is a useful couple of tools.. Maybe you could add functions to return the output in different formats? Above all, have fun with Flash Actionscript 3!