This question already has answers here:
Sorting an array of objects by property values
                                
                                    (30个答案)
                                
                        
                6年前关闭。
            
        

我有一个大问题。我想对这样的数组进行排序:

 '0' ...
        '0' ...
            'id' => "XXXXX"
            'from' ...
                'name' => "XXXX"
                'id' => "XXXXXXXX"
            'story' => "XXXXXXXXXX"
        '1' ...
            'id' => "XXXXX"
            'from' ...
                'name' => "XXXX"
                'id' => "XXXXXXXX"
            'story' => "XXXXXXXXXX"
        '2' ...
            'id' => "XXXXX"
            'from' ...
                'name' => "XXXX"
                'id' => "XXXXXXXX"
            'story' => "XXXXXXXXXX"


我想按密钥FROM-NAME对数组进行排序。
你能帮助我吗?

最佳答案

尝试这个:

yourarray.sort(function(a,b){
    return a.from.name<b.from.name ? -1 : 1;
});

09-17 10:49