data-sort-objects Edge Function — Data
DataSorts an array of objects by one or more fields with configurable direction, null handling, and nested field dot-notation support.
Edge function data-sort-objects Sorts an array of objects by one or more fields with configurable direction, null handling, and nested field dot-notation support.. Deployed on Cloudflare Workers — zero cold starts, globally distributed. Mount it via your Aerostack workspace to call it from any AI agent.
npx aerostack add navin/data-sort-objects Use with AI Assistants
MCPConnect Claude, Cursor, or any MCP-compatible client — then call this function by slug
① Add MCP Server
Add this once — access all Aerostack functions from your AI tool.
{
"mcpServers": {
"aerostack": {
"url": "https://mcp.aerostack.dev",
"type": "http"
}
}
} ② Call this function
Ask your AI to use the call_function tool with this slug:
call_function({
slug: "data-sort-objects",
args: {
"items": null,
"sortBy": null
}
}) data-sort-objects — Multi-key object array sorting
Sorts an array of objects by one or more fields with configurable direction, null handling, and dot-notation for nested fields.
API
POST /api/data-sort-objects
Request body
| Field | Type | Required | Description |
|---|---|---|---|
items |
object[] |
✅ | Array of objects to sort |
sortBy |
SortKey[] |
✅ | Ordered sort keys |
SortKey object:
| Field | Type | Required | Default | Description |
|---|---|---|---|---|
field |
string |
✅ | — | Field name (supports dot notation: "user.age") |
direction |
'asc' | 'desc' |
✅ | — | Sort direction |
nulls |
'first' | 'last' |
❌ | 'last' |
Where to place null/undefined values |
Success response (200)
{
"success": true,
"data": {
"items": [{ "name": "Alice", "age": 25 }, { "name": "Bob", "age": 30 }],
"count": 2
}
}
Error responses
| Code | HTTP | When |
|---|---|---|
INVALID_INPUT |
400 | Invalid sortBy array or missing fields |
INTERNAL_ERROR |
500 | Unexpected error |
Usage
cURL
curl -X POST "$FUNCTION_URL" \
-H "Content-Type: application/json" \
-d '{"items": [{"name":"Bob","age":30},{"name":"Alice","age":25}], "sortBy": [{"field":"name","direction":"asc"}]}'
TypeScript / JavaScript (HTTP)
const response = await fetch(FUNCTION_URL, {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({
items: products,
sortBy: [{ field: 'price', direction: 'asc' }, { field: 'name', direction: 'asc' }],
}),
});
const { data } = await response.json();
console.log(data.items);
Direct import (Node / Bun / Deno)
import { sortObjects } from '@aerostack/functions/data-sort-objects';
const { items } = sortObjects({
items: users,
sortBy: [{ field: 'lastName', direction: 'asc' }, { field: 'firstName', direction: 'asc' }],
});
Use Cases
- Sorting a list of products by price ascending before displaying to a user
- Sorting user records by department then by name for a paginated admin table
- Ordering events by timestamp descending for a feed or timeline view
Notes
- Sorting is stable within equal values (preserves original order for ties at all sort keys)
- Nested field access via dot notation:
"address.city","user.profile.score" - String fields are sorted using
localeComparefor correct Unicode ordering
Metadata
Tags
Publisher
@navin verified
Build and publish your own functions
Write a TypeScript function, deploy it to the edge, and share it with thousands of developers — in minutes.
More Data Functions
Browse Data Functions →data-aggregate-stats
by @navin
Computes descriptive statistics (count, sum, min, max, mean, median, stdDev, variance, percentiles) for an array of numbers.
data-array-group-by
by @navin
Groups an array of objects into a map keyed by the value of a specified property. Zero dependencies.
data-csv-parse
by @navin
Parses a CSV string into an array of row objects with headers, handling quoted fields, custom delimiters, and escaped characters.
data-csv-to-json
by @navin
Converts a CSV string to a typed JSON array, automatically inferring numbers, booleans, and null values from string fields.
data-deep-diff
by @navin
Computes a structural deep diff between two JSON-serializable values, classifying each change as added, removed, or modified.
data-dedupe-array
by @navin
Removes duplicate items from an array using strict equality for primitives, deep equality for objects, or a key field for object arrays.
Frequently asked questions
What does the data-sort-objects function do? +
data-sort-objects is a serverless edge function for data automation written in aerostack. Deploy it to Cloudflare Workers via your Aerostack workspace.
How do I deploy the data-sort-objects function? +
Install the Aerostack CLI and run: ```bash aerostack deploy function @navin/data-sort-objects ``` It will be live on Cloudflare Workers in seconds.
What runtime does data-sort-objects use? +
data-sort-objects runs on aerostack on the Cloudflare Workers edge runtime — zero cold starts, globally distributed.
Can I customise the data-sort-objects function? +
Yes. Fork the function from your Aerostack dashboard, modify the source, and redeploy. All changes are version-controlled.