Back to Insights

Why are my sort orders not working in my SharePoint 2013 search results Web Part?

I’ve recently been working on a project for a public website using SharePoint 2013 and ran into an issue regarding the search results Web Part not applying my defined sort order. One of the requirements for the project was to roll up data from a list that’s sorted by name and location in ascending order. Since I was just rolling up data from a list, I decided to take advantage of the out-of-the-box search results Web Part.

I added a search results Web Part to my page, went into the Web Part properties and setup the query to get data from my list:

SharePoint 2013 search web part

Afterwards I applied my sort order requirement:

SharePoint 2013 search results web part

While I was in the query builder, I ran the query and got results back in the sort order I specified. At this point I was happy, so I saved the Web Part and went back to the page. This is where things got interesting. The Web Part was showing the results from the list but the sort order wasn’t being applied. I went back into the search results Web Part properties and everything was configured fine. I again ran the query in the preview pane and the results were coming back with the proper sort order.

The solution

One solution to this problem would be to create a result source with the query and sorting. You can then configure the Web Part to use that result source. You’d have to manage this outside of your Web Part, so if you don’t feel like adding the overhead of a result source and want all the configuration contained within the web part itself, there is another solution.

The solution is to have the JSON sort defined in the available sort orders property in the Web Part. It’s not the most obvious setting but if you don’t have this defined, your sorting won’t be applied.

How do I add the JSON sort?

To add the JSON go into the search results web part properties, expand the “Settings” toggle and enable the “Show sort dropdown” option. We can get rid of all the predefined sort orders and insert our own:

SharePoint 2013 search results web part

What does the JSON mean?

Let’s format the JSON to get an understanding of what’s going on here. 

 [
   {
      "name":"Name and Location Sort",
      "sorts":[
         {
            "p":"Name",
            "d":0
         },
         {
            "p":"Location",
            "d":0
         }
      ]
   }
]

The “name” property is the display name that you will see in the search drop down. Even though we’re not displaying the search drop down in our Web Part you can still give it a meaningful name.

The “sorts” property contains our sort order for the search results. The “p” is the managed property name and the “d” defines whether descending order is true or false (1 is descending and 0 is ascending).

Knowing what I mentioned above, you can create any sort order you feel like for the search results Web Part. You just have to have the JSON sort order defined in your search results Web Part. For example, if you just want to sort by the name in ascending order then the JSON would look like this:

[
   {
      "name":"Name Sort",
      "sorts":[
         {
            "p":"Name",
            "d":0
         }
      ]
   }
]

Is this a SharePoint bug or is it by design? That’s something that remains unanswered. If you’re working on your SharePoint 2013 solution and find yourself in a position where you have a search results web part configured with a sort order and it isn’t sorting then use the solution I outlined. The nice thing about this solution is that you won’t have to create a result source and all of the configuration is contained within the Web Part itself.

Share