3

I am sending data that I have already sorted by number of counts, from my controller in Laravel to my script file. Json data that I am sending looks like this:

{"5":{"title":"Coop post title","summary":"dbsbsb","count":5},
 "7":{"title":"Example article","summary":"fdsbdfsbsffd","count":5},
 "6":{"title":"Coop's post","summary":"sdbadbb","count":3},
 "0":{"title":"sdvsdv","summary":"dsvsdv","count":2},
 "4":{"title":"sdvsdv","summary":"dsvsdv","count":1}}

But when I parse json data like this in my script file data gets random again:

var cleanData = $.parseJSON(data);
        console.log(cleanData);

And then I get in console data that looks like this:

Object {0: Object, 4: Object, 5: Object, 6: Object, 7: Object}
  0:Object
    count:2
    summary:"dsvsdv"
    title:"sdvsdv"
    __proto__:Object
  4:Object
    count:1
    summary: "dsvsdv"
    title: "sdvsdv"
    __proto__:Object
  5:Object
    count:5
    summary: "dbsbsb"
    title: "Coop post title"
    __proto__: Object
  6:Object
    count:3
    summary: "sdbadbb"
    title: "Coop's post"
    __proto__: Object
  7:Object
    count:5
    summary: "fdsbdfsbsffd"
    title: "Example article"
    __proto__: Object
5
  • 3
    You cannot sort the properties of an object, so your question is moot. If you want to guarantee the order of the objects you would have to use an array. Commented May 24, 2016 at 14:37
  • 1
    I found a good solution stackoverflow.com/questions/4222690/… Commented May 24, 2016 at 14:41
  • It is way better create a json containing an array of objects and simply visit it with a loop, or ES2016 .map method.. Commented May 24, 2016 at 14:42
  • Show the data that you encode. Commented May 24, 2016 at 14:43
  • Possible duplicate of stackoverflow.com/questions/28166518/… Commented May 24, 2016 at 14:46

1 Answer 1

3

You can sort using lodash#sortBy:

_.sortBy(collection, [iteratees=[_.identity]])

Creates an array of elements, sorted in ascending order by the results of running each element in a collection (Array|Object) thru each iteratee. This method performs a stable sort, that is, it preserves the original sort order of equal elements. The iteratees are invoked with one argument: (value).

var cleanData = {"5":{"title":"Coop post title","summary":"dbsbsb","count":5},"7":{"title":"Example article","summary":"fdsbdfsbsffd","count":5},"6":{"title":"Coop's post","summary":"sdbadbb","count":3}, "0":{"title":"sdvsdv","summary":"dsvsdv","count":2}, "4":{"title":"sdvsdv","summary":"dsvsdv","count":1}};

cleanData = _.sortBy(cleanData, ['count']);
console.log(cleanData);
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.13.1/lodash.min.js"></script>

Sign up to request clarification or add additional context in comments.

4 Comments

I get _ is not defined when I am trying to copy your code
Seems like it is sorting it now, but now it is doing it in asc order, how to make it so that it is in desc order?
Found it: cleanData = _.orderBy(cleanData, ['count'], ['desc']);
To get desc: cleanData = _.sortBy(cleanData, ['count']).reverse();

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.