Aerostack
bar_chart

data-sort-objects Edge Function — Data

Data

Sorts an array of objects by one or more fields with configurable direction, null handling, and nested field dot-notation support.

navin @navin verified
Updated Mar 12, 2026
GitHub

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.

terminal — aerostack cli
$ npx aerostack add navin/data-sort-objects
smart_toy

Use with AI Assistants

MCP

Connect 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.

claude_desktop_config.json
{
  "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
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 localeCompare for correct Unicode ordering

Metadata

upgrade Version 1.0.0
gavel License MIT
language Language typescript
cloud Provider aerostack

Tags

data
deployed_code

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 →

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.