Pages

Thursday, December 23, 2010

Javascript Sorting

/**
 * @author : irfanudin ridho
 * @email  : irfan.ub@gmail.com
 * @date   : December 24, 2010
 * @version: 1.0
 *
 * This class provide an algoritm for sorting just two 
 * elements of data
 *
 * @data number, string data provided for sorting.
 */

function Sorted(data){
    this.data = data;
    this.sorted = new Array(2);
}



/**
 * @return Array sorted in ascending Array based on desc
 */


Sorted.prototype.getAsc = function(){
    if(this.data[0]<this.data[1]){
        this.sorted[0] = this.data[0];
        this.sorted[1] = this.data[1];
    }else{
        this.sorted[0] = this.data[1];
        this.sorted[1] = this.data[0];
    }
    return this.sorted;
}



/**
 * @return Array sorted in ascending Array based on desc
 */


Sorted.prototype.getDesc = function(){
    if(this.data[0]<this.data[1]){
        this.sorted[0] = this.data[1];
        this.sorted[1] = this.data[0];
    }else{
        this.sorted[0] = this.data[0];
        this.sorted[1] = this.data[1];
    }
    return this.sorted;
}

No comments:

Post a Comment