> For the complete documentation index, see [llms.txt](https://boxlang.ortusbooks.com/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://boxlang.ortusbooks.com/boxlang-language/reference/built-in-functions/struct/structsort.md).

# StructSort

Sorts a struct according to the specified arguments and returns an array of struct keys

## Method Signature

```
StructSort(struct=[structloose], sortType=[any], sortOrder=[string], path=[string], callback=[function:Comparator])
```

### Arguments

| Argument    | Type                  | Required | Description                                                                                                            | Default |
| ----------- | --------------------- | -------- | ---------------------------------------------------------------------------------------------------------------------- | ------- |
| `struct`    | `struct`              | `true`   | The struct to sort                                                                                                     |         |
| `sortType`  | `any`                 | `false`  | An optional sort type to apply to that type - if a callback is given in this position it will be used as that argument | `text`  |
| `sortOrder` | `string`              | `false`  | The sort order applicable to the sortType argument                                                                     | `asc`   |
| `path`      | `string`              | `false`  |                                                                                                                        |         |
| `callback`  | `function:Comparator` | `false`  | An optional callback to use as the sorting function. You can alternatively pass a Java Comparator.                     |         |

## Examples

### Numeric sorting

[Run Example](https://try.boxlang.io/?code=eJwrzs9NDS4pKk0uUbBVqFbg4gxydVGwUrA01uHijHT18fEPB%2FEMgDz3IFdXPxDHhKvWmqsotbg0B6SnGKw5OL%2BoREOhGG6YjoJSXmlualFmshKQmZJanKykoGnNVV6UWZLqX1pSUApUnZOcWJyqoeAV7O8XDFSZmJNZBeRCDdYEQWsuAHqvL%2B4%3D)

```java
someStruct = { 
	RED : 93,
	YELLOW : 90,
	GREEN : 94
};
result = structSort( someStruct, "numeric", "desc" );
writeOutput( lcase( JSONSerialize( result ) ) );

```

Result: \["green", "red", "yellow"]

### Sort by subelement

[Run Example](https://try.boxlang.io/?code=eJxljs0KwjAQhM%2FNUyw5VSiCCoIWD0VLQbQV0xcIccFCfyTZ4h%2B%2Bu4kUtZQ9LDvsNzOmqVCQbhXBCp6vkJmvMBbrLM%2BdzLwoiWEJ03nAvE18iI75Pk5zq%2FAEa9Sy5KyPJrso7ZGLAama6tIS6gEaZ0fL%2FMGzyQA%2BnO%2BmUF2uRtOWrr%2F5OIhGkw8%2FxwA44Y3qRkmD3F7SKLdOeJGaKqyJwyhkV10QZi3ZUj6U7teHrchSgbqQZfGwZxc0chOyN5rWX20%3D)

```java
someStruct = {};
someStruct.SCOTT = {
	AGE : 26,
	DEPARTMENT : "General"
};
someStruct.GLAN = {
	AGE : 29,
	DEPARTMENT : "computer"
};
someStruct.GEORGE = {
	AGE : 31,
	DEPARTMENT : "Physical"
};
result = structSort( someStruct, "textnocase", "asc", "department" );
writeOutput( lcase( JSONSerialize( result ) ) );

```

Result: \["glan","scott","george"]

### Date sorting using callback

Compare values via dateCompare

```java
birthdays = { 
	"Jim" : "1982/12/5",
	"Anne" : "1968/9/13",
	"Thomas" : "1975/3/28"
};
sorted = structSort( birthdays, ( Any e1, Any e2 ) => {
	return dateCompare( e1, e2 );
} );
for( birthday in sorted ) {
	writeOutput( birthday & " (" & dateDiff( "yyyy", birthdays[ birthday ], now() ) & "), " );
}

```

Result: Anne (49), Thomas (42), Jim (35),

### Additional Examples

[Run Example](https://try.boxlang.io/?code=eJxtjc0KgkAURtczT%2FExKwVBKtoULsQggsBAofVoAwqjxjhThPjuTamE0ur%2BnO%2Bey%2Buy4rJFgA6URPEVO3SUkDROw7PtV2tKeo%2BSy%2Bm4INsBRGG6ABsLaL%2Bnvo%2BkaJ7IjVKi1uDDK3ow1d2B5JmQAYvmkHl4cBWME9yfZVq1jdLihuwF3Wgu57bwf2i0JlqZXCeWOZPOA6tNJVSZ2wzj7bcMN3A%2F798Xg1W0)

```java
animals = { 
	COW : {
		TOTAL : 12
	},
	PIG : {
		TOTAL : 5
	},
	CAT : {
		TOTAL : 3
	}
};
// Show current animals
Dump( label="Current animals", var=animals );
// Show animals sorted by total
Dump( label="Animals sorted by total", var=StructSort( animals, "numeric", "asc", "total" ) );

```

[Run Example](https://try.boxlang.io/?code=eJxdj81uwjAQhM%2FZpxjlFKQI1B6LcgiEG0KVOPVowgZZxDba2CCEePfaUArtyf52ZvbHnNdeQutR4QLKanwgXzq7dTYvKZsl%2FFSih0TzRDOWXt%2FEJuGKT%2Fhysk%2BFRSo0YZN0uk6JJhNsudOW0aq%2BVxvV7tEF23rtLD0%2BNy1JBWp7Br%2BV9%2FcdI1woE%2FZBosmZgxIuoGQXDFs%2FjBfR%2BULRP6UrnUR7boI5FDgqqczPfSXifO6ruH%2FnhDE48dru8hT6F7kH1tFQ4Bl%2FbInRb6u68yx%2FOn0Db5dpWQ%3D%3D)

```java
myStruct = { 
	A : "London",
	B : "Paris",
	C : "Berlin",
	D : "New York",
	E : "Dublin"
};

// define callaback function
function callback( Any e1, Any e2 ) {
	return compare( arguments.E1, arguments.E2 );
}
writeDump( var=myStruct, label="Before sorting" );
writeDump( var=StructSort( myStruct, callback ), label="After sorting" );

```

## Related

* [StructAppend](/boxlang-language/reference/built-in-functions/struct/structappend.md)
* [StructClear](/boxlang-language/reference/built-in-functions/struct/structclear.md)
* [StructCopy](/boxlang-language/reference/built-in-functions/struct/structcopy.md)
* [StructDelete](/boxlang-language/reference/built-in-functions/struct/structdelete.md)
* [StructEach](/boxlang-language/reference/built-in-functions/struct/structeach.md)
* [StructEquals](/boxlang-language/reference/built-in-functions/struct/structequals.md)
* [StructEvery](/boxlang-language/reference/built-in-functions/struct/structevery.md)
* [StructFilter](/boxlang-language/reference/built-in-functions/struct/structfilter.md)
* [StructFind](/boxlang-language/reference/built-in-functions/struct/structfind.md)
* [StructFindKey](/boxlang-language/reference/built-in-functions/struct/structfindkey.md)
* [StructFindValue](/boxlang-language/reference/built-in-functions/struct/structfindvalue.md)
* [StructGet](/boxlang-language/reference/built-in-functions/struct/structget.md)
* [StructGetMetadata](/boxlang-language/reference/built-in-functions/struct/structgetmetadata.md)
* [StructInsert](/boxlang-language/reference/built-in-functions/struct/structinsert.md)
* [StructIsCaseSensitive](/boxlang-language/reference/built-in-functions/struct/structiscasesensitive.md)
* [StructIsOrdered](/boxlang-language/reference/built-in-functions/struct/structisordered.md)
* [StructKeyArray](/boxlang-language/reference/built-in-functions/struct/structkeyarray.md)
* [StructKeyExists](/boxlang-language/reference/built-in-functions/struct/structkeyexists.md)
* [StructKeyList](/boxlang-language/reference/built-in-functions/struct/structkeylist.md)
* [StructKeyTranslate](/boxlang-language/reference/built-in-functions/struct/structkeytranslate.md)
* [StructMap](/boxlang-language/reference/built-in-functions/struct/structmap.md)
* [StructNew](/boxlang-language/reference/built-in-functions/struct/structnew.md)
* [StructNone](/boxlang-language/reference/built-in-functions/struct/structnone.md)
* [StructReduce](/boxlang-language/reference/built-in-functions/struct/structreduce.md)
* [StructSome](/boxlang-language/reference/built-in-functions/struct/structsome.md)
* [StructToQueryString](/boxlang-language/reference/built-in-functions/struct/structtoquerystring.md)
* [StructToSorted](/boxlang-language/reference/built-in-functions/struct/structtosorted.md)
* [StructUpdate](/boxlang-language/reference/built-in-functions/struct/structupdate.md)
* [StructValueArray](/boxlang-language/reference/built-in-functions/struct/structvaluearray.md)


---

# 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 dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://boxlang.ortusbooks.com/boxlang-language/reference/built-in-functions/struct/structsort.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

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.
