Ordering in ASP.NET Core API

Hey there! It’s now time to talk a bit about how to order results with the Mindgaze.AspNetCore library. This article is part of a series talking about the benefits of using Mindgaze.AspNetCore. Previously, we talked about how to perform CRUD operations, filter results and pagination. There are a lot of topics to be discussed and I will take a step by step approach on this so it’s easier to digest and understand.

Good, in order to continue please make sure you have a running app which exposes an endpoint created with EntityController. If not, follow this article to setup one. For this tutorial I’ll use the sample app provided here.

Order by: to ascend or descend, that’s the question

Ok, let’s suppose that our GET http://localhost:5000/bathRoomProducts returns the following:

{
    "pager": {
        "currentPageNumber": 1,
        "currentPageRecords": 6,
        "totalPages": 1,
        "totalRecords": 6,
        "recordsPerPage": 50,
        "nextPageUri": null,
        "prevPageUri": null,
        "firstPageUri": null,
        "lastPageUri": null
    },
    "items": [
        {
            "cleansLimescale": false,
            "name": "haulesBaules",
            "price": 2.899,
            "quantity": 1,
            "id": 2
        },
        {
            "cleansLimescale": false,
            "name": "Domestos",
            "price": 2.899,
            "quantity": 3,
            "id": 3
        },
        {
            "cleansLimescale": true,
            "name": "Mr Muscle",
            "price": 4,
            "quantity": 13,
            "id": 4
        },
        {
            "cleansLimescale": true,
            "name": "Domestos",
            "price": 2.899,
            "quantity": 20,
            "id": 5
        },
        {
            "cleansLimescale": true,
            "name": "Domestos",
            "price": 2.899,
            "quantity": 20,
            "id": 6
        },
        {
            "cleansLimescale": true,
            "name": "Domestos",
            "price": 2.899,
            "quantity": 19,
            "id": 7
        }
    ]
}

We may want this output ordered and in this case we have 2 options

  1. orderByAsc – ascending order by specified member
  2. orderByDesc – descending order by specified member

GET http://localhost:5000/bathRoomProducts?orderByAsc=name

{
    "pager": {
        "currentPageNumber": 1,
        "currentPageRecords": 6,
        "totalPages": 1,
        "totalRecords": 6,
        "recordsPerPage": 50,
        "nextPageUri": null,
        "prevPageUri": null,
        "firstPageUri": null,
        "lastPageUri": null
    },
    "items": [
        {
            "cleansLimescale": false,
            "name": "Domestos",
            "price": 2.899,
            "quantity": 3,
            "id": 3
        },
        {
            "cleansLimescale": true,
            "name": "Domestos",
            "price": 2.899,
            "quantity": 20,
            "id": 5
        },
        {
            "cleansLimescale": true,
            "name": "Domestos",
            "price": 2.899,
            "quantity": 20,
            "id": 6
        },
        {
            "cleansLimescale": true,
            "name": "Domestos",
            "price": 2.899,
            "quantity": 19,
            "id": 7
        },
        {
            "cleansLimescale": false,
            "name": "haulesBaules",
            "price": 2.899,
            "quantity": 1,
            "id": 2
        },
        {
            "cleansLimescale": true,
            "name": "Mr Muscle",
            "price": 4,
            "quantity": 13,
            "id": 4
        }
    ]
}

GET http://localhost:5000/bathRoomProducts?orderByDesc=name

{
    "pager": {
        "currentPageNumber": 1,
        "currentPageRecords": 6,
        "totalPages": 1,
        "totalRecords": 6,
        "recordsPerPage": 50,
        "nextPageUri": null,
        "prevPageUri": null,
        "firstPageUri": null,
        "lastPageUri": null
    },
    "items": [
        {
            "cleansLimescale": true,
            "name": "Mr Muscle",
            "price": 4,
            "quantity": 13,
            "id": 4
        },
        {
            "cleansLimescale": false,
            "name": "haulesBaules",
            "price": 2.899,
            "quantity": 1,
            "id": 2
        },
        {
            "cleansLimescale": false,
            "name": "Domestos",
            "price": 2.899,
            "quantity": 3,
            "id": 3
        },
        {
            "cleansLimescale": true,
            "name": "Domestos",
            "price": 2.899,
            "quantity": 20,
            "id": 5
        },
        {
            "cleansLimescale": true,
            "name": "Domestos",
            "price": 2.899,
            "quantity": 20,
            "id": 6
        },
        {
            "cleansLimescale": true,
            "name": "Domestos",
            "price": 2.899,
            "quantity": 19,
            "id": 7
        }
    ]
}

As you can see this is very easy to use. But we have more products with the same name: what if we want some more ordering of these?
This is where thenByAsc and thenByDesc come in.

GET http://localhost:5000/bathRoomProducts?orderByDesc=name&thenByAsc=quantity

{
    "pager": {
        "currentPageNumber": 1,
        "currentPageRecords": 6,
        "totalPages": 1,
        "totalRecords": 6,
        "recordsPerPage": 50,
        "nextPageUri": null,
        "prevPageUri": null,
        "firstPageUri": null,
        "lastPageUri": null
    },
    "items": [
        {
            "cleansLimescale": true,
            "name": "Mr Muscle",
            "price": 4,
            "quantity": 13,
            "id": 4
        },
        {
            "cleansLimescale": false,
            "name": "haulesBaules",
            "price": 2.899,
            "quantity": 1,
            "id": 2
        },
        {
            "cleansLimescale": false,
            "name": "Domestos",
            "price": 2.899,
            "quantity": 3,
            "id": 3
        },
        {
            "cleansLimescale": true,
            "name": "Domestos",
            "price": 2.899,
            "quantity": 19,
            "id": 7
        },
        {
            "cleansLimescale": true,
            "name": "Domestos",
            "price": 2.899,
            "quantity": 20,
            "id": 5
        },
        {
            "cleansLimescale": true,
            "name": "Domestos",
            "price": 2.899,
            "quantity": 20,
            "id": 6
        }
    ]
}

We can also add more thenByAsc and thenByDesc in the query string, just like in LINQ. I think this is very helpful and powerful as well.

Cool, it’s time to wrap up, I’m gonna take some fresh air and think about the next article. Hope to see you in good health and joyful spirit!

Thanks for reading, I hope you found this article useful and interesting. If you have any suggestions don’t hesitate to contact me. I also invite you to share and subscribe to the newsletter by using the buttons below! Cheers  😉

How useful was this post?

Click on a star to rate it!

Average rating 5 / 5. Vote count: 1

No votes so far! Be the first to rate this post.

We are sorry that this post was not useful for you!

Let us improve this post!

Tell us how we can improve this post?

Leave a Reply

avatar
  Subscribe  
Notify of