0

我想对一个json多级进行排序!!首先按伊斯兰教法排序 -> 然后按 Volume[0] -> 然后按市值 [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

您将需要在每次比较中遍历字段,当第一个键相等时两个元素通过第二个键进行比较,依此类推(请参见此处的 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 回答