# slreportgen.utils.sortObjects

## Syntax

``sortedList = slreportgen.utils.sortObjects(objectList)``
``sortedList = slreportgen.utils.sortObjects(objectList,sortMethod)``

## Description

````sortedList = slreportgen.utils.sortObjects(objectList)` sorts the elements in `objectList` alphabetically by name. You must load the models that contain the objects before using the `sortObjects` function. The function ignores and excludes invalid elements.```

example

````sortedList = slreportgen.utils.sortObjects(objectList,sortMethod)` uses `sortMethod` to sort the elements in `objectList`.```

## Examples

collapse all

This example shows how to use the `slreportgen.utils.sortObjects` function to sort Simulink objects by depth.

Import these packages so you do not have to use long, fully qualified function and class names.

```import slreportgen.finder.* import slreportgen.utils.*```

Load the Simulink model `sortByDepthExampleModel`.

```model_name = "sortByDepthExampleModel"; load_system(model_name)```

Use an `slreportgen.finder.DiagramFinder` object to find all the systems in the model, including the model itself. Then create an empty `slreportgen.finder.BlockResult` array.

```sysArray = find(DiagramFinder(model_name)); unsortedList = BlockResult.empty;```

Iterate through `sysArray` and use an `slreportgen.finder.BlockFinder` object to search for blocks in each subsystem. For each subsystem, append the search result block array to the end of the `unsortedList` array.

```for idx = 1:length(sysArray) curBlockFinder = BlockFinder(sysArray(idx)); curBlockFinder.BlockTypes = ["Inport","Gain","Outport"]; curSysBlocks = find(curBlockFinder); unsortedList(end+1:end+length(curSysBlocks)) = curSysBlocks; end```

Display the paths of the blocks in `unsortedList` to see how they are sorted.

`disp([unsortedList.BlockPath]');`
``` "sortByDepthExampleModel/In0" "sortByDepthExampleModel/Gain0" "sortByDepthExampleModel/Out0" "sortByDepthExampleModel/Sub1/In1" "sortByDepthExampleModel/Sub1/Gain1" "sortByDepthExampleModel/Sub1/Out1" "sortByDepthExampleModel/Sub1/Sub3/In3" "sortByDepthExampleModel/Sub1/Sub3/Gain3" "sortByDepthExampleModel/Sub1/Sub3/Out3" "sortByDepthExampleModel/Sub1/Sub3/Sub4/In4" "sortByDepthExampleModel/Sub1/Sub3/Sub4/Gain4" "sortByDepthExampleModel/Sub1/Sub3/Sub4/Out4" "sortByDepthExampleModel/Sub1/Sub5/In5" "sortByDepthExampleModel/Sub1/Sub5/Gain5" "sortByDepthExampleModel/Sub1/Sub5/Out5" "sortByDepthExampleModel/Sub2/In2" "sortByDepthExampleModel/Sub2/Gain2" "sortByDepthExampleModel/Sub2/Out2" ```

Use the `slreportgen.utils.sortObjects` function to sort the blocks in `unsortedList` by depth. Then display the paths of the sorted blocks to verify they are sorted by depth.

```sortedList = sortObjects(unsortedList,"depth"); disp([sortedList.BlockPath]');```
``` "sortByDepthExampleModel/In0" "sortByDepthExampleModel/Gain0" "sortByDepthExampleModel/Out0" "sortByDepthExampleModel/Sub1/In1" "sortByDepthExampleModel/Sub1/Gain1" "sortByDepthExampleModel/Sub1/Out1" "sortByDepthExampleModel/Sub2/In2" "sortByDepthExampleModel/Sub2/Gain2" "sortByDepthExampleModel/Sub2/Out2" "sortByDepthExampleModel/Sub1/Sub3/In3" "sortByDepthExampleModel/Sub1/Sub3/Gain3" "sortByDepthExampleModel/Sub1/Sub3/Out3" "sortByDepthExampleModel/Sub1/Sub5/In5" "sortByDepthExampleModel/Sub1/Sub5/Gain5" "sortByDepthExampleModel/Sub1/Sub5/Out5" "sortByDepthExampleModel/Sub1/Sub3/Sub4/In4" "sortByDepthExampleModel/Sub1/Sub3/Sub4/Gain4" "sortByDepthExampleModel/Sub1/Sub3/Sub4/Out4" ```

## Input Arguments

collapse all

List of Simulink® and Stateflow® objects to sort, specified as one of these values:

ValueExample
String array of model names and block paths
```objectList = ["slrgex_f14",... "slrgex_f14/Actuator Model",... "slrgex_f14/Aircraft Dynamics Model",... "slrgex_sf_car",... "slrgex_fuelsys",... "slrgex_radar_eml"]; ```
Array of model and block handles
```load_system("slrgex_f14"); objectList = find_system("slrgex_f14",findall=true); ```
Search result object array, returned by `DiagramElementFinder`, `BlockFinder`, `SignalFinder`, and `AnnotationFinder` objects of the `slreportgen.finder` package
```import slreportgen.finder.* load_system("slrgex_sf_car"); load_system("slrgex_fuelsys"); blockFinder = BlockFinder("slrgex_sf_car"); sigFinder = SignalFinder("slrgex_fuelsys"); objectList = [find(blockFinder) find(sigFinder)]; ```

Method for sorting, specified as one of these values:

ValueDescription
`"alphabetical"`

Sort objects alphabetically by name

`"systemAlpha"`

Sort objects alphabetically by parent system name

`"depth"`

Sort objects by depth in the model hierarchy, where a subsystem is preceded by the subsystem that contains it. For an example, see Sort Simulink® Objects by Depth.

## Output Arguments

collapse all

Sorted list, returned as a string array, handle array, or search result object array. The returned array is the same type as `objectList`.

## Version History

Introduced in R2022b