Need to sort text lines programmatically? This guide covers everything you need to know about text sorting via API, including alphabetical, numerical, and custom sorting options.
What is Text Sorting?
Text sorting arranges lines in a specific order based on defined criteria. This is essential for organizing lists, processing data, and preparing text for further analysis. Proper sorting handles case sensitivity, numbers, and special characters correctly.
Example: "Banana", "Apple", "Cherry" sorted alphabetically becomes "Apple", "Banana", "Cherry"
Types of Sorting
Different sorting methods for different needs:
- Alphabetical: A-Z or Z-A ordering
- Numerical: Sort by number value (1, 2, 10, not 1, 10, 2)
- Length: Sort by line length
- Natural: Human-friendly sorting (file1, file2, file10)
Using the Text Sort API
TinyFn provides a simple endpoint to sort text:
POST https://api.tinyfn.io/v1/text/sort
Headers: X-API-Key: your-api-key
Content-Type: application/json
{
"text": "Banana\nApple\nCherry\nDate",
"order": "asc",
"type": "alphabetical"
}
{
"sorted": "Apple\nBanana\nCherry\nDate",
"line_count": 4,
"order": "asc",
"type": "alphabetical"
}
Parameters
| Parameter | Type | Description |
|---|---|---|
text |
string | The text to sort (required) |
order |
string | Sort order: asc or desc (default: asc) |
type |
string | Sort type: alphabetical, numerical, length, natural (default: alphabetical) |
case_sensitive |
boolean | Case-sensitive sorting (default: false) |
Code Examples
JavaScript / Node.js
const response = await fetch(
'https://api.tinyfn.io/v1/text/sort',
{
method: 'POST',
headers: {
'X-API-Key': 'your-api-key',
'Content-Type': 'application/json'
},
body: JSON.stringify({
text: 'Banana\nApple\nCherry',
order: 'asc',
type: 'alphabetical'
})
}
);
const result = await response.json();
console.log(result.sorted); // "Apple\nBanana\nCherry"
Python
import requests
response = requests.post(
'https://api.tinyfn.io/v1/text/sort',
headers={'X-API-Key': 'your-api-key'},
json={'text': 'Banana\nApple\nCherry', 'order': 'asc'}
)
result = response.json()
print(result['sorted']) # "Apple\nBanana\nCherry"
cURL
curl -X POST "https://api.tinyfn.io/v1/text/sort" \
-H "X-API-Key: your-api-key" \
-H "Content-Type: application/json" \
-d '{"text": "Banana\nApple\nCherry", "order": "asc"}'
Common Use Cases
- Name Lists: Alphabetize contact or member lists
- File Lists: Sort filenames naturally
- Data Processing: Sort data before analysis
- Priority Lists: Sort by numerical priority
- Index Generation: Create sorted indexes
Best Practices
- Choose the right type: Use natural sort for mixed text/numbers
- Consider case: Usually case-insensitive is more intuitive
- Handle empty lines: Decide where blanks should appear
- Locale awareness: Consider language-specific sorting rules
Use via MCP
Your AI agent can call this tool directly via Model Context Protocol — no HTTP code needed. Add TinyFn to Claude Desktop, Cursor, or any MCP client:
{
"mcpServers": {
"tinyfn-text": {
"url": "https://api.tinyfn.io/mcp/text/",
"headers": {
"X-API-Key": "your-api-key"
}
}
}
}
See all text analysis tools available via MCP in our Text Analysis MCP Tools for AI Agents guide.