Sorting
Learn how to sort API results to organize data the way you need it.
Overview​
Some list endpoints support sorting by various fields. Specify the sort order using query parameters:
# Sort users by username
GET /accounts/{ACCOUNT_ID}/users?sort=username
Default Sort Order​
Most resources are sorted by creation time (newest first) by default.
CDR Sorting​
Call Detail Records are typically sorted by timestamp:
# Get CDRs sorted by creation time (default)
GET /accounts/{ACCOUNT_ID}/cdrs?created_from=63876672000&created_to=63876758400
# Results are ordered by call start time
Code Examples​
Python - Get Sorted Results​
import requests
API_KEY = 'your_api_key'
ACCOUNT_ID = 'your_account_id'
# Get users
response = requests.get(
f'https://api.audian.com:8443/v2/accounts/{ACCOUNT_ID}/users',
headers={'X-Auth-Token': API_KEY},
params={'page_size': 25}
)
users = response.json()['data']
# Sort client-side by last name
sorted_users = sorted(users, key=lambda u: u.get('last_name', ''))
for user in sorted_users:
print(f"{user.get('last_name')}, {user.get('first_name')}")
Node.js - Client-Side Sorting​
const API_KEY = 'your_api_key';
const ACCOUNT_ID = 'your_account_id';
const response = await fetch(
`https://api.audian.com:8443/v2/accounts/${ACCOUNT_ID}/users`,
{
headers: { 'X-Auth-Token': API_KEY }
}
);
const { data: users } = await response.json();
// Sort by username
users.sort((a, b) => a.username.localeCompare(b.username));
users.forEach(user => {
console.log(`${user.username}: ${user.first_name} ${user.last_name}`);
});
Combining Sorting and Pagination​
When fetching all pages, maintain consistent sort order:
def get_all_users_sorted():
all_users = []
start_key = None
while True:
params = {'page_size': 50}
if start_key:
params['start_key'] = start_key
response = requests.get(
f'https://api.audian.com:8443/v2/accounts/{ACCOUNT_ID}/users',
headers={'X-Auth-Token': API_KEY},
params=params
)
data = response.json()
all_users.extend(data['data'])
if not data.get('next_start_key'):
break
start_key = data['next_start_key']
# Sort all results
return sorted(all_users, key=lambda u: u.get('last_name', ''))
Performance Considerations​
Sort After Filtering​
For best performance, filter data first, then sort:
# Get filtered data
response = requests.get(
f'https://api.audian.com:8443/v2/accounts/{ACCOUNT_ID}/users',
headers={'X-Auth-Token': API_KEY},
params={'filter_status': 'enabled'}
)
users = response.json()['data']
# Sort the filtered results
sorted_users = sorted(users, key=lambda u: u.get('username', ''))
Best Practices​
Do's ✓​
- Filter data to reduce dataset size before sorting
- Use pagination with sorted results
- Cache sorted results if repeated
- Sort client-side when server-side sorting isn't available
Don'ts ✗​
- Don't fetch all data without pagination just to sort
- Don't assume result order without explicit sorting
- Don't ignore the performance impact of sorting large datasets
Next Steps​
- Filtering - Filter results before sorting
- Pagination - Paginate sorted results
- Response Format - Understanding responses