🔍 Code Extractor

class Sort

Maturity: 48

A class representing sort criteria for SharePoint search results, specifying which property to sort on and the sort direction.

File:
/tf/active/vicechatdev/SPFCsync/venv/lib64/python3.11/site-packages/office365/sharepoint/search/query/sort/sort.py
Lines:
4 - 21
Complexity:
simple

Purpose

This class encapsulates sorting information for SharePoint Client Search Query operations. It inherits from ClientValue and is used to define how search results should be ordered by specifying a managed property name and sort direction (ascending or descending). The class is designed to be serialized and sent to SharePoint's search API as part of query parameters.

Source Code

class Sort(ClientValue):
    """Contains information about the property to sort the search results on, and how to sort on the property."""

    def __init__(self, property_name=None, direction=None):
        """
        :param str property_name: If direction is equal to SortDirection.Ascending or SortDirection.Descending,
            then this element specifies the name of the managed property to sort the search results on
        :param int direction: The direction in which to sort on the property specified in the property_name element
        """
        self.Direction = direction
        self.Property = property_name

    def __str__(self):
        return "{0}:{1}".format(self.Property, self.Direction)

    @property
    def entity_type_name(self):
        return "Microsoft.SharePoint.Client.Search.Query.Sort"

Parameters

Name Type Default Kind
bases ClientValue -

Parameter Details

property_name: The name of the SharePoint managed property to sort the search results on. This should be a valid managed property name from the SharePoint search schema. Can be None if not specified initially. Type: str or None.

direction: The direction in which to sort on the specified property. Expected to be an integer value from SortDirection enumeration (typically SortDirection.Ascending or SortDirection.Descending). Can be None if not specified initially. Type: int or None.

Return Value

Instantiation returns a Sort object that contains sorting criteria. The __str__ method returns a string representation in the format 'property_name:direction'. The entity_type_name property returns the fully qualified type name 'Microsoft.SharePoint.Client.Search.Query.Sort' used for SharePoint API serialization.

Class Interface

Methods

__init__(property_name=None, direction=None)

Purpose: Initializes a Sort object with the specified property name and sort direction

Parameters:

  • property_name: The name of the managed property to sort on (str or None)
  • direction: The sort direction as an integer value (int or None)

Returns: None (constructor)

__str__() -> str

Purpose: Returns a string representation of the sort criteria in the format 'property:direction'

Returns: A formatted string showing the property name and direction (e.g., 'Title:0')

@property entity_type_name() -> str property

Purpose: Returns the fully qualified SharePoint entity type name for this class, used for API serialization

Returns: The string 'Microsoft.SharePoint.Client.Search.Query.Sort'

Attributes

Name Type Description Scope
Direction int or None The direction in which to sort (typically 0 for ascending, 1 for descending, or None if not set) instance
Property str or None The name of the SharePoint managed property to sort the search results on instance

Dependencies

  • office365

Required Imports

from office365.runtime.client_value import ClientValue

Usage Example

from office365.runtime.client_value import ClientValue
from office365.sharepoint.search.query.sort import Sort
from office365.sharepoint.search.query.sort_direction import SortDirection

# Create a sort object to sort by 'Title' in ascending order
sort_criteria = Sort(property_name='Title', direction=SortDirection.Ascending)

# String representation
print(sort_criteria)  # Output: Title:0 (assuming Ascending=0)

# Access properties
print(sort_criteria.Property)  # Output: Title
print(sort_criteria.Direction)  # Output: 0

# Get entity type name for serialization
print(sort_criteria.entity_type_name)  # Output: Microsoft.SharePoint.Client.Search.Query.Sort

# Create sort with descending order
sort_desc = Sort('Modified', SortDirection.Descending)

Best Practices

  • Always use valid SharePoint managed property names for the property_name parameter to avoid runtime errors
  • Use the SortDirection enumeration constants (Ascending/Descending) rather than raw integers for the direction parameter to ensure correct values
  • The Sort object is typically used as part of a larger search query configuration, not in isolation
  • The class is immutable after instantiation in typical usage - set properties during construction
  • The entity_type_name property is used internally for SharePoint API serialization and should not be modified
  • Multiple Sort objects can be combined in a collection to create multi-level sorting criteria
  • Ensure the managed property specified is sortable in the SharePoint search schema

Similar Components

AI-powered semantic similarity - components with related functionality:

  • class SortProperty 81.7% similar

    A class representing sort properties for search results, specifying the property name to sort by and the sort direction (ascending or descending).

    From: /tf/active/vicechatdev/SPFCsync/venv/lib64/python3.11/site-packages/office365/search/sort_property.py
  • class SortCollection 78.5% similar

    A collection class for managing sort criteria in SharePoint search queries, allowing addition and removal of sort specifications for search results.

    From: /tf/active/vicechatdev/SPFCsync/venv/lib64/python3.11/site-packages/office365/sharepoint/search/query/sort/collection.py
  • class ReorderingRule 68.6% similar

    ReorderingRule is a class representing SharePoint search result reordering rules, inheriting from ClientValue to provide information about how search results should be reordered when specific conditions are met.

    From: /tf/active/vicechatdev/SPFCsync/venv/lib64/python3.11/site-packages/office365/sharepoint/search/query/reordering_rule.py
  • class SearchEndpoints 64.4% similar

    A class representing SharePoint search endpoints configuration, containing admin endpoint and query context for search operations.

    From: /tf/active/vicechatdev/SPFCsync/venv/lib64/python3.11/site-packages/office365/sharepoint/search/endpoints.py
  • class QueryCondition 64.2% similar

    QueryCondition is a data class that represents conditions for promoted search results in Microsoft Office Server Search REST API.

    From: /tf/active/vicechatdev/SPFCsync/venv/lib64/python3.11/site-packages/office365/sharepoint/search/query/condition.py
← Back to Browse