# 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](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)
* [StructToQueryString](https://boxlang.ortusbooks.com/boxlang-language/reference/built-in-functions/struct/structtoquerystring)
* [StructToSorted](https://boxlang.ortusbooks.com/boxlang-language/reference/built-in-functions/struct/structtosorted)
* [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)
