I have a JSON object:

{ "http://forum.mfd.ru/forum/thread/?id=68203":{ "userHere":1, "title":" Яндекс (Yandex NV)\n \n " }, "http://forum.mfd.ru/forum/thread/?id=84098":{ "userHere":1, "title":" СК ЮЖУРАЛ-АСКО (ACKO)\n \n " }, "http://forum.mfd.ru/forum/thread/?id=67864":{ "userHere":1, "title":" Росгосстрах (РГС, RGSS)\n \n " }, "http://forum.mfd.ru/forum/thread/?id=62700":{ "userHere":1, "title":" Томская распределительная компания (ТРК, TORS)\n \n " }, "http://forum.mfd.ru/forum/thread/?id=64763":{ "userHere":1, "title":" Самараэнерго (SAGO)\n \n " }, "http://forum.mfd.ru/forum/thread/?id=71770":{ "userHere":1, "title":" Сафмар (Европлан, EPLN)\n \n " }, "http://forum.mfd.ru/forum/thread/?id=82762":{ "userHere":1, "title":" Ветка Штирлица\n \n " }, "http://forum.mfd.ru/forum/thread/?id=62126":{ "userHere":1, "title":" Татнефть (TATN)\n \n " }, "http://forum.mfd.ru/forum/thread/?id=77228":{ "userHere":1, "title":" Попуасс вещает и предсказывает.\n \n " }, "http://forum.mfd.ru/forum/thread/?id=60522":{ "userHere":1, "title":" Интер РАО ЕЭС (IRAO)\n \n " }, "http://forum.mfd.ru/forum/thread/?id=62207":{ "userHere":1, "title":" Красный октябрь (KROT)\n \n " }, "http://forum.mfd.ru/forum/thread/?id=71341":{ "userHere":1, "title":" Новороссийский комбинат хлебопродуктов (НКХП, NKHP)\n \n " }, "http://forum.mfd.ru/forum/thread/?id=70940":{ "userHere":1, "title":" Инвестиционный портфель на фондовом рынке - Правила управления\n \n " }, "http://forum.mfd.ru/forum/thread/?id=62482":{ "userHere":2, "title":" МРСК Центра (MRKC)\n \n " }, "http://forum.mfd.ru/forum/thread/?id=61449":{ "userHere":1, "title":" М.Видео (MVID)\n \n " } } 

I need to sort it descending by userHere key value .

  • In javacript, the keys in the objects are out of order, so the sorting should result in an array or Map , but not an ordinary object - diraria
  • @diraria, even so. - DuckerMan

1 answer 1

First, it is not JSON. This is a JavaScript object literal. JSON is a string representation of data that just resembles JavaScript syntax so much.

Secondly, you have an object. They are unsorted. Item order cannot be guaranteed. If you want a guaranteed order, you need to use an array. This will require a change in data structure.

One option might be to make your data look like this:

 var json = [{ "title": "title", "userHere": 3, "url": "url" }, { "title": "title", "userHere": 6, "url": "url" }, { "title": "title", "userHere": 1, "url": "url" }]; 

Now you have an array of objects, and we can sort it.

 json.sort(function(a, b){ return a.userHere - b.userHere; }); 

The resulting array will look like this:

 var json = [{ "title": "title", "userHere": 1, "url": "url" }, { "title": "title", "userHere": 3, "url": "url" }, { "title": "title", "userHere": 6, "url": "url" }]; 

A source

  • The issue is fully resolved. - DuckerMan
  • @DuckerMan put a check mark please) - Tsyklop