Skip to main content
Glama

sort_data

Sort CSV data by specified columns with options for ascending/descending order and row limits to organize your data efficiently.

Instructions

Sort CSV data by specified columns. Args: filename: Name of the CSV file columns: Column name or list of column names to sort by ascending: Whether to sort in ascending order limit: Optional limit on number of rows to return Returns: Dictionary with sorted data

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
ascendingNo
columnsYes
filenameYes
limitNo

Implementation Reference

  • MCP tool handler for 'sort_data' decorated with @mcp.tool(). Delegates to CSVManager.sort_data method.
    @mcp.tool() def sort_data( filename: str, columns: Union[str, List[str]], ascending: bool = True, limit: Optional[int] = None ) -> Dict[str, Any]: """ Sort CSV data by specified columns. Args: filename: Name of the CSV file columns: Column name or list of column names to sort by ascending: Whether to sort in ascending order limit: Optional limit on number of rows to return Returns: Dictionary with sorted data """ try: return csv_manager.sort_data(filename, columns, ascending, limit) except Exception as e: return {"success": False, "error": str(e)}
  • Core sorting implementation in CSVManager using pandas DataFrame.sort_values(). Handles file loading, column validation, sorting, limiting, and returns sorted data.
    def sort_data(self, filename: str, columns: Union[str, List[str]], ascending: bool = True, limit: Optional[int] = None) -> Dict[str, Any]: """Sort CSV data by specified columns.""" filepath = self._get_file_path(filename) if not filepath.exists(): raise FileNotFoundError(f"CSV file '{filename}' not found") try: df = pd.read_csv(filepath) # Ensure columns is a list if isinstance(columns, str): columns = [columns] # Validate columns exist for col in columns: if col not in df.columns: raise ValueError(f"Column '{col}' not found in CSV") # Sort data df_sorted = df.sort_values(by=columns, ascending=ascending) # Apply limit if specified if limit and limit > 0: df_sorted = df_sorted.head(limit) return { "success": True, "filename": filename, "sort_columns": columns, "ascending": ascending, "sorted_data": df_sorted.to_dict('records'), "total_rows": len(df_sorted) } except Exception as e: logger.error(f"Failed to sort data: {e}") raise

Latest Blog Posts

MCP directory API

We provide all the information about MCP servers via our MCP API.

curl -X GET 'https://glama.ai/api/mcp/v1/servers/NovaAI-innovation/csv-mcp-server'

If you have feedback or need assistance with the MCP directory API, please join our Discord server