Ordering with Arrays
Using Y.Array to maintain element order in collaborative applications.
Overview
Y.Array provides ordered sequences that handle concurrent insertions gracefully. Combined with ID-based storage, Y.Array becomes the standard way to represent ordered collections.
The Pattern
Store only IDs in Y.Array; store content in Y.Map. See ID-Based Storage for the complete pattern and core operations.
Concurrent Insert Behavior
When two users insert at the same position simultaneously:
The order of X and Y is determined by client IDs (arbitrary but consistent). Both items appear—neither is lost.
Grouped Ordering
For items grouped into categories, use a Map of Arrays:
Moving an item between groups: delete ID from source array, insert into target array, update item’s groupId field—all in one transaction.
Performance Tips
Large arrays (10,000+ items):
- Cache
order.toArray()instead of calling repeatedly - Use UI pagination/virtualization
Batch operations:
See Also
- ID-Based Storage - Content/order separation
- Transactions - Batching changes