2

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
        }
    ]
}
Susheel Singh
  • 3,824
  • 5
  • 31
  • 66
Legomite
  • 99
  • 2
  • 7

3 Answers3

0

JavaScript has an array sort: http://www.w3schools.com/jsref/jsref_sort.asp

Never mind, it's been pointed out to me that this link doesn't have any pertinent examples. Nevertheless, I do think sort is what you're looking for.

Damon Kaswell
  • 1,270
  • 1
  • 10
  • 16
0

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);
flagoworld
  • 3,196
  • 2
  • 20
  • 16
0

You can actually specify your own comparation function for the sort method of the array. The comparator function should receive 2 parameters and return:

  • -1, if the first parameter should go before the second in the sorted array.
  • 0, if you consider both parameters to be equal (either one can go before).
  • 1, if the first parameter should go after the second parameter.

Using that, you can create your own, ad hoc comparator function and sort the array using your own criterion. As an example, you can see here a function for sorting your array based on its name property and another one for sorting based on its views property:

function compareByName(a, b) {
    var namea = a.name, nameb = b.name;
    for(var i = 0; i < namea.length && i < nameb.length; i++) {
        if(namea.charCodeAt(i) !== nameb.charCodeAt(i))
            return namea.charCodeAt(i) - nameb.charCodeAt(i);
    }
    return namea.length - nameb.length;
}

function compareByViews(a, b) {
    return a.views - b.views;
}

With those functions created and with your object containing the articles stored in data, you can sort it like this:

data.articles.sort(compareByName); //sort by name
data.articles.sort(compareByViews); //sort by views

You can see a full example working here.

Hawkings
  • 533
  • 3
  • 16