0

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="Металлургия и производство готовых металлических продуктов"

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

  • 2
    How do you wish to sort them? – George Reith Aug 14 '13 at 10:18
  • 4
    This is not even valid JavaScript. It's certainly not an array of objects. This pseudo code could describe an array of strings or an object with string properties. Please provide a correct example. I also don't see how this is related to jQuery at all. – Felix Kling Aug 14 '13 at 10:20
  • http://stackoverflow.com/questions/5503900/how-to-sort-an-array-of-objects-with-jquery-or-javascript – rags Aug 14 '13 at 10:21
  • 1
    Do you mean `{1:"Металлургия...",2:"Химическа.."}` ? – MDEV Aug 14 '13 at 10:23
  • @GeorgeReith I have provided full code – Galymzhan Aigelov Aug 14 '13 at 11:16
  • possible duplicate of [Sorting an array of JavaScript objects](http://stackoverflow.com/questions/979256/sorting-an-array-of-javascript-objects) – Bergi Aug 14 '13 at 13:09

1 Answers1

1

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

e.g. 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/

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.

George Reith
  • 13,132
  • 18
  • 79
  • 148
  • @B.K. What do you mean? – George Reith Aug 14 '13 at 10:42
  • @B.K. It's saying don't change their order if they're the same (the function will already have returned `-1` or `1` if a change of order was required) – MDEV Aug 14 '13 at 11:03
  • @B.K. `return 0` means do not modify the order in this iteration. This is because if neither of the if conditions are met the two parameters must be equal. The rest of the array still sorts. – George Reith Aug 14 '13 at 11:04
  • @GeorgeReith my array not in this way, I've used console.log and it showed me in this way 1="Химическая промышленность", 2="...", ... – Galymzhan Aigelov Aug 14 '13 at 11:23
  • @GalymzhanAigelov Your original question asked to sort an array of objects. You have an array of strings. See my updated answer. – George Reith Aug 14 '13 at 12:34