> For the complete documentation index, see [llms.txt](https://docs.flatredball.com/flatredball/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.flatredball.com/flatredball/api/flatredball/math/geometry/shapecollection/sortascending.md).

# SortAscending

### Introduction

The SortAscending method sorts all contained elements in the ShapeCollection by their position values on the given axis. In other words, if this method is called with an argument of Axis.X, then all contained shapes will be sorted so that their X values are increasing. The sort uses a stable insertion sort making it incredibly fast on nearly-sorted or already-sorted lists.

### Common usage

ShapeCollections are often sorted so that axis-based partitioning can be performed when testing for collisions. If the ShapeCollection will not change after being created, then it only needs to be sorted once (likely when it is first created). Otherwise, it needs to be sorted whenever a change occurs - which may be as frequent as every frame. However, sorting every frame and using partitioning is likely faster than performing a brute-force collision check.

### Code Example

The following code can be used to sort a ShapeCollection along its X axis, then test for collisions using axis-based partitioning:

```
// assuming myShapeCollection is a valid ShapeCollection:
Axis sortAxis = Axis.X;
myShapeCollection.SortAscending(sortAxis);
// Assuming myCharacter has a property called Collision which is a shape:
bool usePartitioning = true;
// The max radii must be calculated before calling a partitionined CollideAgainst method
myShapeCollection.CalculateMaxRadii();
if(myShapeCollection.CollideAgainst(myCharacter.Collision, usePartitioning, sortAxis))
{
   // React to the collision here
}
```

Note that CalculateMaxRadii must be called in order for the ShapeCollection to know the maximum radii of all shapes (by category).

### Additional information

* [Axis Based Partitioning and Collision](https://github.com/flatredball/FlatRedBallDocs/tree/main/frb/docs/index.php) - Talks about axis-based partitioning which is what the CollideAgainst uses internally when performing partitioned collisions.


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation by asking a question.

Perform an HTTP GET request on the following URL with the `ask` and `goal` query parameters:

```
GET https://docs.flatredball.com/flatredball/api/flatredball/math/geometry/shapecollection/sortascending.md?ask=<question>&goal=<user_goal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is what the user is ultimately trying to achieve, the reason they need the answer. Sharing it helps GitBook give you a better, more relevant answer. A goal is most helpful when it describes the outcome the user wants rather than restating the question. For example, with `ask=how do I create an API token`, a goal like `automate deployments from our CI pipeline` lets GitBook tailor the answer to that use case.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
