0

This is my JSON structure :

Root
|- cells []
     |-Individual cells containing the following
                         |- Facts (Object)
                         |- Measures (Object)
                                 |- Key values pairs
|- other values

I need to sort the values in the measures object in-place. Will it be possible? If not, what should be the optimized way to approach this? I could have those values extracted into a temporary array and then sort them, and then search for their respective keys and re-order the entire JSON. But that doesn't seem an optimized solution.

I'm basically trying to achieve sorting in the jsHypercube : https://github.com/thesmart/js-hypercube . Searched the library, but couldn't find any sort method.


Adding a snap of sample JSON : enter image description here

Expected output is a sorted array of _cells based on any key in the measures object, like Cost, Profit or Revenue.


As per the comments of @FelixKling , I believe the data should be sorted before serializing it into a JavaScript Object. The original data before serialization : enter image description here

Kazekage Gaara
  • 14,972
  • 14
  • 61
  • 108
  • We cannot sort the elements in the objects, because they are technically hashtables. – thefourtheye Jan 20 '14 at 06:33
  • Could you please provide same input JSON and the expected output? – thefourtheye Jan 20 '14 at 06:34
  • 3
    Please note that JSON is a language-independent, textual data exchange format, much like XML, CSV or YAML. You cannot *sort* JSON by itself, but you can sort the data structures to which the JSON encoded data was converted to. So: Your problem has nothing to do with JSON. See also: [There's no such thing as a "JSON Object"](http://benalman.com/news/2010/03/theres-no-such-thing-as-a-json/) – Felix Kling Jan 20 '14 at 06:36
  • If possible try to sort before convert into JSON. – Sureshkumar Panneerselvan Jan 20 '14 at 06:37
  • @thefourtheye Please look at the edit. I've added a snap of the JSON as shown in my console. – Kazekage Gaara Jan 20 '14 at 06:39
  • 2
    Again: This is not JSON, this is a JavaScript object. – Felix Kling Jan 20 '14 at 06:39
  • @FelixKling thanks for the information and the link. Definitely worth a read. – Kazekage Gaara Jan 20 '14 at 06:40
  • @KazekageGaara Thanks :) Now, for the displayed object, please update the question with expected output as well. – thefourtheye Jan 20 '14 at 06:40
  • Of course if you have the data stored as JSON and want to find a way to sort it without parsing it, then I don't think that would be possible. FYI, the native array sort method is already in-place: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/sort. – Felix Kling Jan 20 '14 at 06:42
  • @FelixKling have a look at the edit. Perhaps I now understand your point. And I can try the `sort()` method on this. – Kazekage Gaara Jan 20 '14 at 06:48
  • @thefourtheye please have a look at the edit. – Kazekage Gaara Jan 20 '14 at 06:48
  • *"I believe the data should be serialized before serializing it into a JavaScript Object"* isn't correct. You typically serialize a native data type (such as an object) to some kind of string representation (such as JSON). You can serialize something to a JavaScript object. But anyway, it looks like you have have an array of objects, and you want to sort that array by a certain property? – Felix Kling Jan 20 '14 at 06:51
  • @FelixKling that was a typo. My bad. And yes, I want to sort it by a certain property. I'm trying to find a solution to it, as this seems achievable rather than trying to sort it after serialization. – Kazekage Gaara Jan 20 '14 at 06:53
  • 1
    I guess your question is a duplicate of http://stackoverflow.com/q/1129216/218196 and http://stackoverflow.com/q/979256/218196 then ([and some more](http://stackoverflow.com/search?q=[javascript]+sort+array+of+objects)). – Felix Kling Jan 20 '14 at 06:54
  • Thinking of Objects, im asking myself why "I need to sort the values in the measures object". There's basically no need in having properties in a particular order as long as I'm using objects as what they are. What are you trying to achieve which needs sorting of properties of an object? – Axel Amthor Jan 20 '14 at 07:26
  • @AxelAmthor for OLAP transactions for dashboards on client side, there might be a need for sorting as well. For all other major functionality, I'm using jshypercube. But it didn't have a sort method. Hence, the need. – Kazekage Gaara Jan 20 '14 at 07:29

1 Answers1

1

With help of @FelixKling's suggestion, I was able to sort it using a simple block of code. Posting it since it might be helpful for someone else.

tempCube.sort(function(a, b) {
        var valueA, valueB;        

        valueA = a.measures.Cost; 
        valueB = b.measures.Cost;
        if (valueA < valueB) {
            return -1;
        }
        else if (valueA > valueB) {
            return 1;
        }
        return 0;
    });
Kazekage Gaara
  • 14,972
  • 14
  • 61
  • 108