r.r
r.r

Reputation: 7153

how to sort array by groups with javascript?

I have an Array with 12 Entries. I like to sort them by titles. Many of articles have the same titles, and i could be easy, because title are identical. i could be probably 3 or 4 groups at the end.

i have very tiny javascript code, because i dont know if i want to do it with loop or some other way.

Javascript

 var totalGroups = [];
        var actualGroup = [];

var contentarticles = articles.contentarticles,
                article,
                $out = $("#articlesOutput");


                for (var i = 0; i < contentarticles.length; i++) {
                    if (!article || article.title != contentarticles[i].title) {
                        article = contentarticles[i];

                        document.getElementById('articleForNaviTopTitle').innerHTML = article.title;
                        document.getElementById('articleForNaviTopID').innerHTML = article.id;

                        var articlesOutput = [
                            '<li><a href="./certifiedTraining/id=', article.id, '/step=', i + 1, '">',
                            article.title,
                            '</li>'
                        ].join("");
                        $out.append(articlesOutput);
                    }
                }
// till this point all works fine, and a code above gives results in the image below.

//**Im struggeling right there, how to sort this array by groups?????**

while (article.title(this) == article.title(next))
  {
  code block to be executed
  }

enter image description here

Upvotes: 2

Views: 7176

Answers (2)

ZER0
ZER0

Reputation: 25322

Not sure if I understand what you mean about "sorting array by groups". Let's say you want to sort an array by title; then you will have something like:

contentarticles.sort(function(a, b) {
    if (a.title > b.title) return 1;
    if (a.title < b.title) return -1;
    return 0;
 });

But if this is not the result you want, could you be more explicit? What do you want do to exactly, filter out the duplicates? Creates "array of array"? Or what?

If you want to group instead of sort:

var articles = contentarticles.reduce(function(articles, article) {
    (articles[article.title] || (articles[article.title] = [])).push(article);
    return articles;
}, {});

In this way you will have an object where the properties are the title it self, an each property will contains an array with the full article object. So, once you have the title, you can obtain all the articles with the same title. If you want a list of all titles, you can use Object.keys; where if you want to iterate you can use for…in.

Here the reduce method I used.

Upvotes: 5

Garlov
Garlov

Reputation: 124

If I understood correctly what you want is to pick out every object and sort them in groups by title. If I were you I'd take a look at UnderScore js. It is a nice collection of utilities for js.

var grouped = _.groupBy(yourObjects, 'title');

Upvotes: 5

Related Questions