简体   繁体   English

如何在jQuery中对对象数组进行排序

[英]How to sort array of objects in jQuery

I have an array in this way 我以这种方式有一个数组

{
    1="Металлургия и производство готовых металлических продуктов",
    2="Химическая промышленность",
    3="Альтернативная энергетика",
    4="Транспортная инфраструктура"
}

I've looked another questions like this, but couldn't find anything. 我已经看了另一个这样的问题,但找不到任何东西。

Full code: 完整代码:

$.getJSON('/project/'+ clicked +'s/', function(data) {

            var info =
                '<td></td>'
                +'<td>'
                    +'<select name="'+ clicked +'_id'+ id +'" id="edit_'+ clicked +'_id'+ id +'">'
                        +'<option value="">'+obj['list_'+lang]+'</option>';

                        for (var i in data) {
                                info += '<option value="'+ i +'">'+ data[i] +'</option>';
                        }
                    info += '</select>'
                +'</td>';

            $('#'+ clicked +'s'+ id).show().html(info);

        });

In data I have an array which is above. 在数据中,我有一个上面的数组。 I want to sort them alphabetically like this: 3="Альтернативная энергетика" 1="Металлургия и производство готовых металлических продуктов" 我想按字母顺序对它们进行排序:3 =“Альтернативнаяэнергетика” 1 =“Металлургияипроизводствоготовыхметаллическихпродуктов”

class Controller_Project extends Controller_Website {
public function action_sectors() {
    $sectors = ORM::factory('sector')
        ->find_all()
        ->as_array('id', 'name');

    $ar_sectors = array();
    switch ($this->user_language) {
        case 'ru':
            $ar_sectors[15] = 'Агропромышленный комплекс';
            $ar_sectors[3] = 'Альтернативная энергетика';
            $ar_sectors[7] = 'АПК и текстильная промышленность';
            $ar_sectors[13] = 'Атомная промышленность и атомная энергетика';
            break;
        case 'en':
            $ar_sectors[15] = 'Agricultural Sector';
            $ar_sectors[3] = 'Alternative Energy Industry';
            $ar_sectors[7] = 'Agriculture and textiles';
            $ar_sectors[13] = 'Atomic Industry';
            break;
    }

    $sectors = $ar_sectors;
    $this->auto_render = FALSE;
    $this->request->response = json_encode($sectors);

}

} }

The site where code is situated http://new.baseinvest.kz/project 代码所在的网站http://new.baseinvest.kz/project

You can use JavaScript's native sort method and pass your own comparative function, depending on how you wish to sort them: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/sort 您可以使用JavaScript的本机sort方法并传递自己的比较函数,具体取决于您希望对它们进行排序的方式: https : //developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/sort

eg To sort by a property of each object alphabetically 例如按字母顺序按每个对象的属性排序

var arr = [{say: "hi"}, {say: "bye"}];

arr.sort(function(a, b) {
   if (a.say > b.say) {
      return 1;
   }
   if (a.say < b.say) {
      return -1;
   }
   return 0;
});

// arr now equals [{say: "bye"}, {say: "hi"}];

Working example: http://jsfiddle.net/uKAEz/ 工作示例: http : //jsfiddle.net/uKAEz/

Edit: In response to your updates it is clear you are not sorting an array of objects but are sorting an array of strings. 编辑:响应您的更新,很显然您不是在对对象数组进行排序,而是对字符串数组进行排序。 It also appears they are in Russian. 看来它们也用俄语。 The following should do what you want: 以下应该做您想要的:

arr.sort(function(a, b) {
    return a.localeCompare(b, "ru");
});

See: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/localeCompare for information on localCompare which facilitates sorting unicode strings. 请参阅: https//developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Global_Objects/String/localeCompare,以获取有关localCompare信息,该信息有助于对Unicode字符串进行排序。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM