JavaScript Sorting JSON items that meets certain criterias

I'm trying to sort the JSON by release date, by random, by alphabetical order or by popularity. I'm not sure if the answer is on here, I checked and I can't seem to figure out if it's for me.

My JavaScript jQuery code grabs the JSON file for me:

$.getJSON("js/example.json", function(data) {
    $.each(data.articles, function() {
        //code
    });
});

I want JavaScript to collect the data from all the items in the articles and sort them. After that, it could probably be added into the HTML through a for() or something better.

Here is what the JSON looks like.

{
    "articles":[
        {
            "name":"aaa",
            "date":"10/13/15",
            "views":65
        },
        {
            "name":"ccc",
            "date":"10/17/15",
            "views":175
        },
        {
            "name":"bbb",
            "date":"10/11/15",
            "views":54
        },
        {
            "name":"ddd",
            "date":"10/17/15",
            "views":6
        }
    ]
}
Answer:1

Fiddle:

https://jsfiddle.net/ppLso0mg/

You can usually utilize the someArr.sort() function, which takes a comparator function. The function works by giving you a and b which you have to determine the order of.

Here is an example of 3 different comparators for 3 different properties on your JSON object. I'll let you use your imagination on how you could clump them together into one big function. But this should at least illustrate how it works.

var json = 
[
{
"name": "aaa",
"date": "10/13/15",
"views": 65
},
{
"name": "ccc",
"date": "10/17/15",
"views": 175
},
{
"name": "bbb",
"date": "10/11/15",
"views": 54
},
{
"name": "ddd",
"date": "10/17/15",
"views": 6
}
];

var sortFunctions =
{
    name: function(a, b)
    {
        return a.name.localeCompare(b.name);
    },
    date: function(a, b)
    {
        if(a.date == b.date)
        {
            return 0;
        }

        var aDate = new Date(a.date);
        var bDate = new Date(b.date);

        return aDate > bDate ? 1 : -1;
    },

    views: function(a, b)
    {
        if(a.views == b.views)
        {
            return 0;
        }

        return a.views > b.views ? 1 : -1;
    }
};

json.sort(sortFunctions.name);
console.log(json);

json.sort(sortFunctions.date);
console.log(json);

json.sort(sortFunctions.views);
console.log(json);
Answer:2

I try to create a new pieces page, kind of a blog style in apostrophe cms. I have trainings, trainings-widgets and trainings-pages folders. I registered them all in app.js; pieces by themselves and ...

I try to create a new pieces page, kind of a blog style in apostrophe cms. I have trainings, trainings-widgets and trainings-pages folders. I registered them all in app.js; pieces by themselves and ...

  1. apostrophe pieces pages

I am passing a state as props in child component, but child is updating previous props to its state. For example: I am passing state = a, then state is updated to state = b. Child will update it as ...

I am passing a state as props in child component, but child is updating previous props to its state. For example: I am passing state = a, then state is updated to state = b. Child will update it as ...

I have the below line of java script, which aims to split a string and replace the value at the splitted section with the new value. I appreciate this could be done with a 2-dimensional array, but I ...

I have the below line of java script, which aims to split a string and replace the value at the splitted section with the new value. I appreciate this could be done with a 2-dimensional array, but I ...

var people = [ ["nan", "30", "female", ["4545345454", "4324324324"]], ["ban", "35", "male", ["4324234234", "7576343243"]], ["san", "38", "male", ["3242342343", "3423423434"]] ]; //var k=0; ...

var people = [ ["nan", "30", "female", ["4545345454", "4324324324"]], ["ban", "35", "male", ["4324234234", "7576343243"]], ["san", "38", "male", ["3242342343", "3423423434"]] ]; //var k=0; ...