# 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`  |                                                                                                                        | `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](https://boxlang.ortusbooks.com/boxlang-language/reference/built-in-functions/struct/structappend)
* [StructClear](https://boxlang.ortusbooks.com/boxlang-language/reference/built-in-functions/struct/structclear)
* [StructCopy](https://boxlang.ortusbooks.com/boxlang-language/reference/built-in-functions/struct/structcopy)
* [StructDelete](https://boxlang.ortusbooks.com/boxlang-language/reference/built-in-functions/struct/structdelete)
* [StructEach](https://boxlang.ortusbooks.com/boxlang-language/reference/built-in-functions/struct/structeach)
* [StructEquals](https://boxlang.ortusbooks.com/boxlang-language/reference/built-in-functions/struct/structequals)
* [StructEvery](https://boxlang.ortusbooks.com/boxlang-language/reference/built-in-functions/struct/structevery)
* [StructFilter](https://boxlang.ortusbooks.com/boxlang-language/reference/built-in-functions/struct/structfilter)
* [StructFind](https://boxlang.ortusbooks.com/boxlang-language/reference/built-in-functions/struct/structfind)
* [StructFindKey](https://boxlang.ortusbooks.com/boxlang-language/reference/built-in-functions/struct/structfindkey)
* [StructFindValue](https://boxlang.ortusbooks.com/boxlang-language/reference/built-in-functions/struct/structfindvalue)
* [StructGet](https://boxlang.ortusbooks.com/boxlang-language/reference/built-in-functions/struct/structget)
* [StructGetMetadata](https://boxlang.ortusbooks.com/boxlang-language/reference/built-in-functions/struct/structgetmetadata)
* [StructInsert](https://boxlang.ortusbooks.com/boxlang-language/reference/built-in-functions/struct/structinsert)
* [StructIsCaseSensitive](https://boxlang.ortusbooks.com/boxlang-language/reference/built-in-functions/struct/structiscasesensitive)
* [StructIsOrdered](https://boxlang.ortusbooks.com/boxlang-language/reference/built-in-functions/struct/structisordered)
* [StructKeyArray](https://boxlang.ortusbooks.com/boxlang-language/reference/built-in-functions/struct/structkeyarray)
* [StructKeyExists](https://boxlang.ortusbooks.com/boxlang-language/reference/built-in-functions/struct/structkeyexists)
* [StructKeyList](https://boxlang.ortusbooks.com/boxlang-language/reference/built-in-functions/struct/structkeylist)
* [StructKeyTranslate](https://boxlang.ortusbooks.com/boxlang-language/reference/built-in-functions/struct/structkeytranslate)
* [StructMap](https://boxlang.ortusbooks.com/boxlang-language/reference/built-in-functions/struct/structmap)
* [StructNew](https://boxlang.ortusbooks.com/boxlang-language/reference/built-in-functions/struct/structnew)
* [StructNone](https://boxlang.ortusbooks.com/boxlang-language/reference/built-in-functions/struct/structnone)
* [StructReduce](https://boxlang.ortusbooks.com/boxlang-language/reference/built-in-functions/struct/structreduce)
* [StructSome](https://boxlang.ortusbooks.com/boxlang-language/reference/built-in-functions/struct/structsome)
* [StructSort](https://boxlang.ortusbooks.com/boxlang-language/reference/built-in-functions/struct/structsort)
* [StructToQueryString](https://boxlang.ortusbooks.com/boxlang-language/reference/built-in-functions/struct/structtoquerystring)
* [StructUpdate](https://boxlang.ortusbooks.com/boxlang-language/reference/built-in-functions/struct/structupdate)
* [StructValueArray](https://boxlang.ortusbooks.com/boxlang-language/reference/built-in-functions/struct/structvaluearray)


---

# Agent Instructions: 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:

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

The question should be specific, self-contained, and written in natural language.
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.
