2

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

4
  • Do you want to sort, or aggregate into buckets? Also, you seem to be having some encoding issues for non-ASCII characters. Commented Oct 25, 2013 at 9:27
  • look at the image in the right side. its just a array list 0 till 11. but i want to have it sorted in groups. in one group should be articles with the same title. maybe i should work with object and not array? Commented Oct 25, 2013 at 9:31
  • In computer programming, "sorting" usually means "ordering". It sounds like you actually mean "collecting". Commented Oct 25, 2013 at 9:37
  • BTW, images in posts are annoying, since we can't cut and paste them into the Javascript console. Please post Javascript literals showing what you have and what you're trying to get. Commented Oct 25, 2013 at 9:39

2 Answers 2

5

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');
Sign up to request clarification or add additional context in comments.

1 Comment

-> greate Solution! very simple
5

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.

8 Comments

look at the image in the right side. its just a array list 0 till 11. but i want to have it sorted in groups. in one group should be articles with the same title. maybe i should work with object and not array? in my array should be at least 3 such a groups where the titles are the same.
It's clear what you have now, but not which structure you want to obtain. I will try to make some example in the answer, but you have to be a bit more explicit.
Nice code -- I've never thought of using reduce for this, I've always written this kind of thing with an explicit loop with if in it.
ok ZERO i will test it how it works, and i will give you now! i think i used wrong term "sort" i needed just say "ordering"
Notice that in this case is not "ordering", at least to me. It's just "collecting" the item with the same property under the same group. So it's more "grouping". But let me know if this is what you expected. Notice I updated the code because a mistake.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.