Search code examples
linq.js

orderBy a grouped collection in linq.js


Thats the code which does not work:

var grouping = Enumerable.from(periods)
                    .select(function(p){return new PeriodViewModel(p)})
                    .groupBy("$.periodNumber")
                    .orderBy("$.date")
                    .toArray();

the periodviewmodels are grouped by periodNumber and the date should be ordered ascending by the date, but that does not happen and I guess its because the selector in orderBy should somehow refer to the grouping result...

I have no clue. Thats test data so you can try it for yourself:

{
    "periods": [
        {
            "id": 1,
            "content": "this is not a test",
            "date": 1,
            "periodNumber": 1,
            "documents": [
                {
                    "name": "Homework.doc"
                },
                {
                    "name": "Todo.xls"
                }
            ]
        },
        {
            "id": 3,
            "content": "this might be a test",
            "date": 2,
            "periodNumber": 1,
            "documents": [
                {
                    "name": "Homework.doc"
                },
                {
                    "name": "Todo.xls"
                }
            ]
        },
        {
            "id": 5,
            "content": "This is a clause",
            "date": 3,
            "periodNumber": 1,
            "documents": [
                {
                    "name": "Homework.doc"
                },
                {
                    "name": "Todo.xls"
                }
            ]
        },
        {
            "id": 1,
            "content": "and not sth to share for all",
            "date": 5,
            "periodNumber": 2,
            "documents": [
                {
                    "name": "Homework.doc"
                },
                {
                    "name": "Todo.xls"
                }
            ]
        },
        {
            "id": 2,
            "content": "hello kitty???",
            "date": 2,
            "periodNumber": 3,
            "documents": [
                {
                    "name": "Homework.doc"
                },
                {
                    "name": "Todo.xls"
                }
            ]
        },
        {
            "id": 1,
            "content": "Flynnie winnie",
            "date": 1,
            "periodNumber": 3,
            "documents": [
                {
                    "name": "Homework.doc"
                },
                {
                    "name": "Todo.xls"
                }
            ]
        },
        {
            "id": 1,
            "content": "what do yo say about chol and ra?",
            "date": 1,
            "periodNumber": 4,
            "documents": [
                {
                    "name": "Homework.doc"
                },
                {
                    "name": "Todo.xls"
                }
            ]
        }
    ]
}

Solution

  • Sort the items before you group them. The date is not accessible from the group as you have it.

    var query = Enumerable.from(periods)
        .select(function (p){ return new PeriodViewModel(p); })
        .orderBy("$.date")
        .groupBy("$.periodNumber")
        .toArray();