> 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/structtosorted.md).

# StructToSorted

Converts a struct to a sorted struct - using either a callback comparator or textual directives as the sort option

## Method Signature

```
StructToSorted(struct=[structloose], sortType=[any], sortOrder=[string], localeSensitive=[any], 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`   |
| `localeSensitive` | `any`                 | `false`  | Sort based on local rules                                                                                              | `false` |
| `callback`        | `function:Comparator` | `false`  | An optional callback to use as the sorting function. You can alternatively pass a Java Comparator.                     |         |

## Examples

### structToSorted with an inline function sorting by key value

Use a function to sort the keys by numeric value descending

[Run Example](https://try.boxlang.io/?code=eJx9kE9rwkAQxc%2BZTzF4ihAPiVBog4JiQEFyaMT2KptRFs2u7B8lSL57dxPd0ktvvzdv3vAYLRuqjLLM4AwfCNGo3JTFCD%2FwPXGi2nx7fvO8W38WvTPt1Vex3fcyzbw2inQwNXEdcsLSjcLFWjIaUtDlAEcrmOFSoJbKLNvSNqQ42x8ulvSKNCNRc3GKcSFavPlpmvxyNvCZ2jRQhmN8QMSP8XMf58%2FlwYgUGasETtIcog5e0qkOfAeqwzsG2MmqH8eu4utXyf91cZzDXXEXss01xj9nnfUDVyhvuQ%3D%3D)

```java
someStruct = { 
	"NINE" : 9,
	"SIX" : 6,
	"THREE" : 3,
	"TWELVE" : 12,
	"tres" : 3,
	"seis" : 6,
	"nueve" : 9,
	"doce" : 12
};

function sortByNumericValuesDescending( Any value1, Any value2, Any key1, Any key2 ) {
	if( value1 > value2 ) {
		return -1;
	}
	return 1;
}
sortedStruct = StructToSorted( someStruct, sortByNumericValuesDescending );
writedump( sortedStruct );

```

Result: A new struct with the keys ordered by value descending

### structToSorted member function with an inline function sorting by key value

Use a function to sort the keys by numeric value descending

[Run Example](https://try.boxlang.io/?code=eJx9kM2KwkAQhM%2Fpp2g8RVAhEQQ3KCgGFCQHI7rXZdLKoJmR%2BVGC5N3NJDre9lZfV3VTtJYl5UZZZnCGT4Sgl22ytIc%2FOB00kG9%2BnZ44vV%2Fv0tYZt3RMt4cWo9ixUaS9qYlrvycs3clfLCSjbgvqBOBkBTNcCtRSmWWV2ZIUZ4e%2FqyW9Is1IFFycQ1yICu9uGg2%2BOu70harIqxj7%2BISAn8J3HufvcGcEioxVAodRAkENH2yoBteBCv8O7X8z2su89cL%2Fe2I%2FgYfiTdCWty7r7zXWC0yjbRY%3D)

```java
someStruct = { 
	"NINE" : 9,
	"SIX" : 6,
	"THREE" : 3,
	"TWELVE" : 12,
	"tres" : 3,
	"seis" : 6,
	"nueve" : 9,
	"doce" : 12
};

function sortByNumericValuesDescending( Any value1, Any value2, Any key1, Any key2 ) {
	if( value1 > value2 ) {
		return -1;
	}
	return 1;
}
sortedStruct = someStruct.ToSorted( sortByNumericValuesDescending );
writedump( sortedStruct );

```

Result: A new struct with the keys ordered by value descending

### structToSorted member function with an inline function sorting by key name

Use a function to sort the keys by name

```java
someStruct = { 
	"NINE" : 9,
	"SIX" : 6,
	"THREE" : 3,
	"TWELVE" : 12,
	"tres" : 3,
	"seis" : 6,
	"nueve" : 9,
	"doce" : 12
};

function sortByKeyName( Any value1, Any value2, Any key1, Any key2 ) {
	return compareNoCase( key1, key2 );
}
sortedStruct = someStruct.ToSorted( sortByKeyName );
writedump( sortedStruct );

```

Result: A new struct with the keys ordered by key name

### structToSorted member function with sorttype argument

Use a function to sort the keys by name

[Run Example](https://try.boxlang.io/?code=eJwrzs9NDS4pKk0uUbBVqFbg4lTy8%2FRzVVKwUrDUAXKCPSNAbDMQO8QjyBUsYwzmhbv6hIG5hkYgfklRajFcsjg1sxiuL680tSwVbmJKfnIqRBdXrTVXcX5RSWoK3AHFcNfoheQHg%2BU0FJRKUitKlHQUlBKLk4FUWmJOcaqCpjVXeVEmUL40t0BDAcUYoBQAaYE6eQ%3D%3D)

```java
someStruct = { 
	"NINE" : 9,
	"SIX" : 6,
	"THREE" : 3,
	"TWELVE" : 12,
	"tres" : 3,
	"seis" : 6,
	"nueve" : 9,
	"doce" : 12
};
sortedStruct = someStruct.ToSorted( "text", "asc", false );
writedump( sortedStruct );

```

Result: A new struct with the keys ordered by key name

### Additional Examples

[Run Example](https://try.boxlang.io/?code=eJxtz8kKwjAQBuBz8hRDThU8uIIoPbice6k%2BQGynWugSp1O1iO9u1CrUSA7hn%2BTLTPImwIoxDpnqiMGHG0ihtDEZKpjDTQqhDKXRK437z3iqdcEpN8%2FKcCDF3VbVXhd2uWTokGkroiMSNa4YOWLWiljzn6Em7lAjC%2BR9IS%2BUMm7q3Hhw1uTnnb%2F2IdN7zHy1wqQkhKokTouDgt4H7j7wDbZlaK9g7MHvQ4rxykG51hUqm2KsIrvbQ4Tet80yYaROlwfpCG78)

```java
myNestedStruct = { 
	"apple" : {
		"price" : 3,
		"quantity" : 10
	},
	"banana" : {
		"price" : 1,
		"quantity" : 5
	},
	"cherry" : {
		"price" : 2,
		"quantity" : 8
	},
	"date" : {
		"price" : 4,
		"quantity" : 12
	}
};
writeDump( var=myNestedStruct, label="Before sorting" );
writeDUmp( var=StructToSorted( myNestedStruct, "textNoCase", "desc", true ), label="After sorting" );

```

[Run Example](https://try.boxlang.io/?code=eJxdj0FvgkAUhM%2FyKyacILGQXfWEmGDszXih9g74MKQsmMduDTH9711CkOhtvsmbyTzVn4zKgzViuBs3ctTIq4HlzHLg1cxiYGG5yK3ykDQ9frPakFjOWo76h3rxVBI%2B4h0ezqIqPWR8NYoa3QXfyfH8KbB9tyTCEFVAAS7UFc7Cxpm04QYiskR1R5PxYZ2%2FyLlzpelg1M2zKzgeBy9RZznVsbunsmVC17KumqsLfwqcp0Cq2RT6q03tCV08TAX2U%2F9Zk5Sa%2BKXlH2GmY9A%3D)

```java
myNumb.4 = "5";
myNumb.3 = "2";
myNumb.2 = "3";
myNumb.1 = "1";
cb = ( Any value1, Any value2, Any key1, Any key2 ) => {
	if( arguments.VALUE1 < arguments.VALUE2 // i.e. desc
	 ) return 1;
	 else return -1;
};
writeDump( var=myNumb, label="Before sorting" );
writeDUmp( var=StructToSorted( myNumb, cb ), 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)
* [StructSort](/boxlang-language/reference/built-in-functions/struct/structsort.md)
* [StructToQueryString](/boxlang-language/reference/built-in-functions/struct/structtoquerystring.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 by asking a question.

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

```
GET https://boxlang.ortusbooks.com/boxlang-language/reference/built-in-functions/struct/structtosorted.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.
