0

jsonマルチレベルを並べ替えたい!! 最初にShariaで並べ替え、次にVolume [0]で並べ替え、次にmarketcap[0]で並べ替えます

ソートされていない:

[
    {
        "Name": "Hcl",
        "sharia": true,
        "Volume": [
            25000,
            "moderate"
        ],
        "marketcap": [
            5000,
            "large"
        ]
    },
    {
        "Name": "rado",
        "sharia": false,
        "Volume": [
            50000,
            "high"
        ],
        "marketcap": [
            5000,
            "small"
        ]
    },
    {
        "Name": "ssinc ",
        "sharia": false,
        "Volume": [
            200,
            "low"
        ],
        "marketcap": [
            5000,
            "large"
        ]
    },
    {
        "Name": "sureshot",
        "sharia": false,
        "Volume": [
            20000,
            "moderate"
        ],
        "marketcap": [
            5000,
            "small"
        ]
    },
    {
        "Name": "Infosys",
        "sharia": true,
        "Volume": [
            50000,
            "high"
        ],
        "marketcap": [
            6000,
            "large"
        ]
    },
    {
        "Name": "orchid",
        "sharia": true,
        "Volume": [
            200,
            "low"
        ],
        "marketcap": [
            6000,
            "Large"
        ]
    },
    {
        "Name": "hero honda",
        "sharia": true,
        "Volume": [
            150,
            "low"
        ],
        "marketcap": [
            400,
            "medium "
        ]
    },
    {
        "Name": "bioffoz",
        "sharia": false,
        "Volume": [
            40000,
            "moderate"
        ],
        "marketcap": [
            5000,
            "large"
        ]
    }
]

今のところ、私は以下の関数を使用して一度ソートしています:

var sort_by = function(field, reverse, primer){

   var key = function (x) {return primer ? primer(x[field]) : x[field]};

   return function (a,b) {
       var A = key(a), B = key(b);
      return (A < B ? -1 : (A > B ? 1 : 0)) * [1,-1][+!!reverse];                
   }
}

A-Zのソート用

   company.sort(sort_by('Name', false, function(a){return a.toUpperCase()}));

ブール値のソートの場合:

company.sort(sort_by('sharia', true, function(a){return Boolean(a)}));

高から低に並べ替える場合:

company.sort(sort_by('sharia', true, parseint));

シャリーアで並べ替え(次のようになります):

[
    {
        "Name": "Hcl",
        "sharia": true,
        "Volume": [
            25000,
            "moderate"
        ],
        "marketcap": [
            5000,
            "large"
        ]
    },
    {
        "Name": "orchid",
        "sharia": true,
        "Volume": [
            200,
            "low"
        ],
        "marketcap": [
            6000,
            "Large"
        ]
    },
    {
        "Name": "hero honda",
        "sharia": true,
        "Volume": [
            150,
            "low"
        ],
        "marketcap": [
            400,
            "medium "
        ]
    },
    {
        "Name": "Infosys",
        "sharia": true,
        "Volume": [
            50000,
            "high"
        ],
        "marketcap": [
            6000,
            "large"
        ]
    },
    {
        "Name": "ssinc ",
        "sharia": false,
        "Volume": [
            200,
            "low"
        ],
        "marketcap": [
            5000,
            "large"
        ]
    },
    {
        "Name": "sureshot",
        "sharia": false,
        "Volume": [
            20000,
            "moderate"
        ],
        "marketcap": [
            5000,
            "small"
        ]
    },
    {
        "Name": "rado",
        "sharia": false,
        "Volume": [
            50000,
            "high"
        ],
        "marketcap": [
            5000,
            "small"
        ]
    },
    {
        "Name": "bioffoz",
        "sharia": false,
        "Volume": [
            40000,
            "moderate"
        ],
        "marketcap": [
            5000,
            "large"
        ]
    }
]

次に、ボリュームで並べ替えます

[
    {
        "Name": "Infosys",
        "sharia": true,
        "Volume": [
            50000,
            "high"
        ],
        "marketcap": [
            6000,
            "large"
        ]
    },
    {
        "Name": "Hcl",
        "sharia": true,
        "Volume": [
            25000,
            "moderate"
        ],
        "marketcap": [
            5000,
            "large"
        ]
    },
    {
        "Name": "orchid",
        "sharia": true,
        "Volume": [
            200,
            "low"
        ],
        "marketcap": [
            6000,
            "Large"
        ]
    },
    {
        "Name": "hero honda",
        "sharia": true,
        "Volume": [
            150,
            "low"
        ],
        "marketcap": [
            400,
            "medium "
        ]
    },
    {
        "Name": "rado",
        "sharia": false,
        "Volume": [
            50000,
            "high"
        ],
        "marketcap": [
            5000,
            "small"
        ]
    },
    {
        "Name": "bioffoz",
        "sharia": false,
        "Volume": [
            40000,
            "moderate"
        ],
        "marketcap": [
            5000,
            "large"
        ]
    },
    {
        "Name": "sureshot",
        "sharia": false,
        "Volume": [
            20000,
            "moderate"
        ],
        "marketcap": [
            5000,
            "small"
        ]
    },
    {
        "Name": "ssinc ",
        "sharia": false,
        "Volume": [
            200,
            "low"
        ],
        "marketcap": [
            5000,
            "large"
        ]
    }
]

すべてソート済み:

[
    {
        "Name": "Infosys",
        "sharia": true,
        "Volume": [
            50000,
            "high"
        ],
        "marketcap": [
            6000,
            "large"
        ]
    },
    {
        "Name": "Hcl",
        "sharia": true,
        "Volume": [
            25000,
            "moderate"
        ],
        "marketcap": [
            5000,
            "large"
        ]
    },
    {
        "Name": "orchid",
        "sharia": true,
        "Volume": [
            200,
            "low"
        ],
        "marketcap": [
            6000,
            "Large"
        ]
    },
    {
        "Name": "hero honda",
        "sharia": true,
        "Volume": [
            150,
            "low"
        ],
        "marketcap": [
            400,
            "medium "
        ]
    },
    {
        "Name": "rado",
        "sharia": false,
        "Volume": [
            50000,
            "high"
        ],
        "marketcap": [
            5000,
            "small"
        ]
    },
    {
        "Name": "bioffoz",
        "sharia": false,
        "Volume": [
            40000,
            "moderate"
        ],
        "marketcap": [
            5000,
            "large"
        ]
    },
    {
        "Name": "sureshot",
        "sharia": false,
        "Volume": [
            20000,
            "moderate"
        ],
        "marketcap": [
            5000,
            "small"
        ]
    },
    {
        "Name": "ssinc ",
        "sharia": false,
        "Volume": [
            200,
            "low"
        ],
        "marketcap": [
            5000,
            "large"
        ]
    }
]
4

1 に答える 1

1

2つの要素が最初のキーで等しい場合、2番目のキーで比較するなど、各比較でフィールドを反復処理する必要があります(ifステートメントを使用してここを参照してください。一般的な解決策を以下に示します)。また、一部のフィールドがネストされているため、さらに複雑になります(動作については、これらのいずれかを参照してください)。

function sort_by_multiple(fields, reverse, primer){

    function key(x, field) {
        if (Array.isArray(field))
            for (var i=0; i<field.length; i++)
                x = x[field[i]];
        else
            x = x[field];
        return primer ? primer(x) : x;
    }

    return function (a,b) {
        for (var cmp=0, i=0; i<fields.length && cmp==0; i++) {
            var A = key(a, fields[i]), B = key(b, fields[i]);
            cmp = (A>B) - (B>A);
        }
        return reverse ? -cmp : cmp;
    }
}

今、あなたはすることができます:

company.sort(sort_by_multiple(['sharia',  ['Volume',0], ['marketcap',0]], true);
于 2013-02-23T11:20:06.923 に答える