December 3, 2024

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! Don’t forget to checkout the source code for the sample here. Also the source of the Mindgaze.AspNetCore library.

Thanks for reading, I hope you found this article useful and interesting. If you have any suggestions don’t hesitate to contact me. If you found my content useful please consider a small donation. Any support is greatly appreciated! Cheers  😉

afivan

Enthusiast adventurer, software developer with a high sense of creativity, discipline and achievement. I like to travel, I like music and outdoor sports. Because I have a broken ligament, I prefer safer activities like running or biking. In a couple of years, my ambition is to become a good technical lead with entrepreneurial mindset. From a personal point of view, I’d like to establish my own family, so I’ll have lots of things to do, there’s never time to get bored 😂

View all posts by afivan →