【JavaScript】sortメソッド その3

  • このエントリーをはてなブックマークに追加

sortを使って配列を並び替える。
配列の要素がオブジェクトの場合のsortを考える。
配列sの要素がオブジェクトの場合にオブジェクトのプロパティ名「first」の値で昇順に並び替える。



firstの値で昇順に並び替える比較関数CompareArray001は次の通り。


const s = [{
        first: "Joe",
        last: "DeRita",
        aa: "zz",
        kk: "wq"
    },
    {
        first: "ZCurly",
        last: "Howard",
        aa: "ab",
        kk: "bb"
    },
    {
        first: "ACurly",
        last: "Howard",
        aa: "ab",
        kk: "aa"
    },
    {
        first: "Joe",
        last: "DeRita",
        aa: "ab",
        kk: "zz"
    },
    {
        first: "Moe",
        last: "Howard",
        aa: "ab",
        kk: "aa"
    },
    {
        first: "Joe",
        last: "Besser",
        aa: "ab",
        kk: "aa"
    },
    {
        first: "Shemp",
        last: "Howard",
        aa: "ab",
        kk: "aa"
    },
    {
        first: "Larry",
        last: "Fine",
        aa: "ab",
        kk: "aa"
    },
    {
        first: "Curly",
        last: "Howard",
        aa: "ab",
        kk: "aa"
    },
    {
        first: "BCurly",
        last: "Howard",
        aa: "ab",
        kk: "zz"
    },
    {
        first: "Joe",
        last: "DeRita",
        aa: "ab",
        kk: "bb"
    },
    {
        first: "Joe",
        last: "DeRita",
        aa: "ab",
        kk: "vv"
    },
    {
        first: "Joe",
        last: "DeRita",
        aa: "cc",
        kk: "aa"
    },
    {
        first: "Joe",
        last: "Ab",
        aa: "ab",
        kk: "cc"
    }
];

const CompareArray001 = function(array) {

    var by = function(name) {

        var that = function(a, b) {
            try {

                if (!a || !b || typeof a !== "object" || typeof b !== "object") {

                    throw {
                        name: "Error",
                        message: "プロパティ名に" + name + "を持つオブジェクトが必要です。typeof a =" + typeof a + ", a = " + a + ", typeof b =" + typeof b + ", b = " + b
                    };

                } else {

                    const valueA = a[name];
                    const valueB = b[name];

                    if (valueA === valueB) {
                        return 0;
                    } else if (typeof valueA === typeof valueB) {
                        return valueA < valueB ? -1 : 1;
                    } else {
                        return typeof valueA < typeof valueB ? -1 : 1;
                    }
                }

            } catch (e) {
                console.error(e.name + ":" + e.message);
            }

        };

        return that;
    };

    return by("first")

};

console.log("--元の配列--");
s.forEach(function(value, index, array) {
    console.log("first = " + s[index].first + ", last = " + s[index].last + ", aa = " + s[index].aa + ", kk = " + s[index].kk);
})

console.log("--sort後の配列--");
//CompareArray001関数で並び替える。
//firstの値で昇順に並び替える。
s.sort(CompareArray001("first")).forEach(function(value, index, array) {
    console.log("first = " + s[index].first + ", last = " + s[index].last + ", aa = " + s[index].aa + ", kk = " + s[index].kk);
})

/*
--元の配列-- 
first = Joe, last = DeRita, aa = zz, kk = wq 
first = ZCurly, last = Howard, aa = ab, kk = bb 
first = ACurly, last = Howard, aa = ab, kk = aa 
first = Joe, last = DeRita, aa = ab, kk = zz 
first = Moe, last = Howard, aa = ab, kk = aa 
first = Joe, last = Besser, aa = ab, kk = aa 
first = Shemp, last = Howard, aa = ab, kk = aa 
first = Larry, last = Fine, aa = ab, kk = aa 
first = Curly, last = Howard, aa = ab, kk = aa 
first = BCurly, last = Howard, aa = ab, kk = zz 
first = Joe, last = DeRita, aa = ab, kk = bb 
first = Joe, last = DeRita, aa = ab, kk = vv 
first = Joe, last = DeRita, aa = cc, kk = aa 
first = Joe, last = Ab, aa = ab, kk = cc 
--sort後の配列-- 
first = ACurly, last = Howard, aa = ab, kk = aa 
first = BCurly, last = Howard, aa = ab, kk = zz 
first = Curly, last = Howard, aa = ab, kk = aa 
first = Joe, last = DeRita, aa = zz, kk = wq 
first = Joe, last = DeRita, aa = ab, kk = zz 
first = Joe, last = Besser, aa = ab, kk = aa 
first = Joe, last = DeRita, aa = ab, kk = bb 
first = Joe, last = DeRita, aa = ab, kk = vv 
first = Joe, last = DeRita, aa = cc, kk = aa 
first = Joe, last = Ab, aa = ab, kk = cc 
first = Larry, last = Fine, aa = ab, kk = aa 
first = Moe, last = Howard, aa = ab, kk = aa 
first = Shemp, last = Howard, aa = ab, kk = aa 
first = ZCurly, last = Howard, aa = ab, kk = bb
*/

firstの値で昇順に並び替え、firstが同じ値ならばlastの値で昇順に並び替える。
比較関数CompareArray001を改造した比較関数CompareArray002は次の通り。


const CompareArray002 = function(x, y) {

    var by = function(name, minor) {

        var that = function(a, b) {

            try {

                if (!a || !b || typeof a !== "object" || typeof b !== "object") {

                    throw {
                        name: "Error",
                        message: "プロパティ名に" + name + "を持つオブジェクトが必要です。typeof a =" + typeof a + ", a = " + a + ", typeof b =" + typeof b + ", b = " + b
                    };

                } else {

                    const valueA = a[name];
                    const valueB = b[name];

                    if (valueA === valueB) {
                        return typeof minor === "function" ? minor(a, b) : 0;
                    } else if (typeof valueA === typeof valueB) {
                        return valueA < valueB ? -1 : 1;
                    } else {
                        return typeof valueA < typeof valueB ? -1 : 1;
                    }

                }

            } catch (e) {
                console.error(e.name + ":" + e.message);
            }

        };

        return that;

    };

    return by(x, by(y))

};

console.log("--sort後の配列--");
//CompareArray002関数で並び替える。
//firstの値で昇順に並び替え、firstが同じ値ならばlastの値で昇順に並び替える。
s.sort(CompareArray002("first", "last")).forEach(function(value, index, array) {
    console.log("first = " + s[index].first + ", last = " + s[index].last + ", aa = " + s[index].aa + ", kk = " + s[index].kk);
})

/*
--元の配列-- 
first = Joe, last = DeRita, aa = zz, kk = wq 
first = ZCurly, last = Howard, aa = ab, kk = bb 
first = ACurly, last = Howard, aa = ab, kk = aa 
first = Joe, last = DeRita, aa = ab, kk = zz 
first = Moe, last = Howard, aa = ab, kk = aa 
first = Joe, last = Besser, aa = ab, kk = aa 
first = Shemp, last = Howard, aa = ab, kk = aa 
first = Larry, last = Fine, aa = ab, kk = aa 
first = Curly, last = Howard, aa = ab, kk = aa 
first = BCurly, last = Howard, aa = ab, kk = zz 
first = Joe, last = DeRita, aa = ab, kk = bb 
first = Joe, last = DeRita, aa = ab, kk = vv 
first = Joe, last = DeRita, aa = cc, kk = aa 
first = Joe, last = Ab, aa = ab, kk = cc 
--sort後の配列-- 
first = ACurly, last = Howard, aa = ab, kk = aa 
first = BCurly, last = Howard, aa = ab, kk = zz 
first = Curly, last = Howard, aa = ab, kk = aa 
first = Joe, last = Ab, aa = ab, kk = cc 
first = Joe, last = Besser, aa = ab, kk = aa 
first = Joe, last = DeRita, aa = zz, kk = wq 
first = Joe, last = DeRita, aa = ab, kk = zz 
first = Joe, last = DeRita, aa = ab, kk = bb 
first = Joe, last = DeRita, aa = ab, kk = vv 
first = Joe, last = DeRita, aa = cc, kk = aa 
first = Larry, last = Fine, aa = ab, kk = aa 
first = Moe, last = Howard, aa = ab, kk = aa 
first = Shemp, last = Howard, aa = ab, kk = aa 
first = ZCurly, last = Howard, aa = ab, kk = bb
*/


firstの値で昇順に並び替え、firstが同じ値ならばlastの値で昇順に並び替え、lastが同じ値ならばaaの値で昇順に並び替え、aaが同じ値ならばkkの値で昇順に並び替える。
この改造によりオブジェクトのプロパティが何個あっても対応可能となる。
比較関数CompareArray002を改造した比較関数CompareArray003は次の通り。



const CompareArray003 = function(array) {

    const by = function(t) {

        const name = t.name;
        const minor = t.minor;

        const that = function(a, b, i) {

            try {

                if (!a || !b || typeof a !== "object" || typeof b !== "object") {

                    throw {
                        name: "Error",
                        message: "プロパティ名に" + name + "を持つオブジェクトが必要です。typeof a =" + typeof a + ", a = " + a + ", typeof b =" + typeof b + ", b = " + b
                    };

                } else {

                    if (typeof i === "undefined") {
                        i = 0;
                    }

                    const valueA = a[name[i]];
                    const valueB = b[name[i]];

                    if (valueA === valueB) {
                        return typeof minor === "function" ? minor(a, b, i + 1) : 0;
                    } else if (typeof valueA === typeof valueB) {
                        return valueA < valueB ? -1 : 1;
                    } else {
                        return typeof valueA < typeof valueB ? -1 : 1;
                    }

                }

            } catch (e) {
                console.error(e.name + ":" + e.message);
            }

        };

        return that;

    };

    //firstの値で昇順に並び替える。
    //return by({"name": array,"minor": null});
    //firstの値で昇順に並び替え、firstが同じ値ならばlastの値で昇順に並び替える。
    //return by({"name": array,"minor": by({"name": array,"minor": null})});
    //firstの値で昇順に並び替え、firstが同じ値ならばlastの値で昇順に並び替え、lastが同じ値ならばaaの値で昇順に並び替える。
    //return by({"name": array,"minor": by({"name": array,"minor": by({"name": array,"minor": null})})});
    //firstの値で昇順に並び替え、firstが同じ値ならばlastの値で昇順に並び替え、lastが同じ値ならばaaの値で昇順に並び替え、aaが同じ値ならばkkの値で昇順に並び替える。
    return by({"name": array,"minor": by({"name": array,"minor": by({"name": array,"minor": by({"name": array,"minor": null})})})});
};

console.log("--sort後の配列--");
//CompareArray003関数で並び替える。
//firstの値で昇順に並び替え、firstが同じ値ならばlastの値で昇順に並び替え、lastが同じ値ならばaaの値で昇順に並び替え、aaが同じ値ならばkkの値で昇順に並び替える。
s.sort(CompareArray003(["first", "last", "aa", "kk"])).forEach(function(value, index, array) {
    console.log("first = " + s[index].first + ", last = " + s[index].last + ", aa = " + s[index].aa + ", kk = " + s[index].kk);
})

/*
--元の配列-- 
first = Joe, last = DeRita, aa = zz, kk = wq 
first = ZCurly, last = Howard, aa = ab, kk = bb 
first = ACurly, last = Howard, aa = ab, kk = aa 
first = Joe, last = DeRita, aa = ab, kk = zz 
first = Moe, last = Howard, aa = ab, kk = aa 
first = Joe, last = Besser, aa = ab, kk = aa 
first = Shemp, last = Howard, aa = ab, kk = aa 
first = Larry, last = Fine, aa = ab, kk = aa 
first = Curly, last = Howard, aa = ab, kk = aa 
first = BCurly, last = Howard, aa = ab, kk = zz 
first = Joe, last = DeRita, aa = ab, kk = bb 
first = Joe, last = DeRita, aa = ab, kk = vv 
first = Joe, last = DeRita, aa = cc, kk = aa 
first = Joe, last = Ab, aa = ab, kk = cc 
--sort後の配列-- 
first = ACurly, last = Howard, aa = ab, kk = aa 
first = BCurly, last = Howard, aa = ab, kk = zz 
first = Curly, last = Howard, aa = ab, kk = aa 
first = Joe, last = Ab, aa = ab, kk = cc 
first = Joe, last = Besser, aa = ab, kk = aa 
first = Joe, last = DeRita, aa = ab, kk = bb 
first = Joe, last = DeRita, aa = ab, kk = vv 
first = Joe, last = DeRita, aa = ab, kk = zz 
first = Joe, last = DeRita, aa = cc, kk = aa 
first = Joe, last = DeRita, aa = zz, kk = wq 
first = Larry, last = Fine, aa = ab, kk = aa 
first = Moe, last = Howard, aa = ab, kk = aa 
first = Shemp, last = Howard, aa = ab, kk = aa 
first = ZCurly, last = Howard, aa = ab, kk = bb
*/

firstの値で昇順に並び替え、firstが同じ値ならばlastの値で昇順に並び替え、lastが同じ値ならばaaの値で昇順に並び替え、aaが同じ値ならばkkの値で昇順に並び替える。
オブジェクトにプロパティを追加すると比較関数CompareArray003を修正する必要があるので、オブジェクトにプロパティを追加しても比較関数CompareArray003を修正不要になるように再帰関数を使用するように改造する。
比較関数CompareArray003を改造した比較関数CompareArray004は次の通り。
比較関数CompareArray004はオブジェクトのプロパティが増えた場合は、比較関数CompareArray004の引数の配列の要素にプロパティを追加すれば、追加したプロパティの値でソートが可能になる。


const CompareArray004 = function(array) {

    const by = function(t) {

        const name = t.name;
        const minor = t.minor;

        const that = function(a, b, i) {

            try {

                if (!a || !b || typeof a !== "object" || typeof b !== "object") {

                    throw {
                        name: "Error",
                        message: "プロパティ名に" + name + "を持つオブジェクトが必要です。typeof a =" + typeof a + ", a = " + a + ", typeof b =" + typeof b + ", b = " + b
                    };

                } else {

                    if (typeof i === "undefined") {
                        i = 0;
                    }

                    const valueA = a[name[i]];
                    const valueB = b[name[i]];

                    if (valueA === valueB) {
                        return typeof minor === "function" ? minor(a, b, i + 1) : 0;
                    } else if (typeof valueA === typeof valueB) {
                        return valueA < valueB ? -1 : 1;
                    } else {
                        return typeof valueA < typeof valueB ? -1 : 1;
                    }

                }

            } catch (e) {
                console.error(e.name + ":" + e.message);
            }

        };

        return that;

    };

    const fnc = function(t, array) {
        //再帰関数を使う
        const innerFnc = function(u) {
            return u === 0 ? by({
                "name": array,
                "minor": null
            }) : by({
                "name": array,
                "minor": innerFnc(u - 1)
            });
        };
        return innerFnc(t);
    };

    return fnc(array.length - 1, array);
};

console.log("--sort後の配列--");
//CompareArray004関数で並び替える。
//firstの値で昇順に並び替え、firstが同じ値ならばlastの値で昇順に並び替え、lastが同じ値ならばaaの値で昇順に並び替え、aaが同じ値ならばkkの値で昇順に並び替える。
//再帰関数を使用する。
s.sort(CompareArray004(["first", "last", "aa","kk"])).forEach(function(value, index, array) {
    console.log("first = " + s[index].first + ", last = " + s[index].last + ", aa = " + s[index].aa + ", kk = " + s[index].kk);
})

/*
--元の配列-- 
first = Joe, last = DeRita, aa = zz, kk = wq 
first = ZCurly, last = Howard, aa = ab, kk = bb 
first = ACurly, last = Howard, aa = ab, kk = aa 
first = Joe, last = DeRita, aa = ab, kk = zz 
first = Moe, last = Howard, aa = ab, kk = aa 
first = Joe, last = Besser, aa = ab, kk = aa 
first = Shemp, last = Howard, aa = ab, kk = aa 
first = Larry, last = Fine, aa = ab, kk = aa 
first = Curly, last = Howard, aa = ab, kk = aa 
first = BCurly, last = Howard, aa = ab, kk = zz 
first = Joe, last = DeRita, aa = ab, kk = bb 
first = Joe, last = DeRita, aa = ab, kk = vv 
first = Joe, last = DeRita, aa = cc, kk = aa 
first = Joe, last = Ab, aa = ab, kk = cc 
--sort後の配列-- 
first = ACurly, last = Howard, aa = ab, kk = aa 
first = BCurly, last = Howard, aa = ab, kk = zz 
first = Curly, last = Howard, aa = ab, kk = aa 
first = Joe, last = Ab, aa = ab, kk = cc 
first = Joe, last = Besser, aa = ab, kk = aa 
first = Joe, last = DeRita, aa = ab, kk = bb 
first = Joe, last = DeRita, aa = ab, kk = vv 
first = Joe, last = DeRita, aa = ab, kk = zz 
first = Joe, last = DeRita, aa = cc, kk = aa 
first = Joe, last = DeRita, aa = zz, kk = wq 
first = Larry, last = Fine, aa = ab, kk = aa 
first = Moe, last = Howard, aa = ab, kk = aa 
first = Shemp, last = Howard, aa = ab, kk = aa 
first = ZCurly, last = Howard, aa = ab, kk = bb
*/


  • このエントリーをはてなブックマークに追加

SNSでもご購読できます。

コメントを残す

*