---
title: "sortSubviews(_:context:)"
framework: appkit
role: symbol
role_heading: Instance Method
path: "appkit/nsview/sortsubviews(_:context:)"
---

# sortSubviews(_:context:)

Orders the view’s immediate subviews using the specified comparator function.

## Declaration

```swift
func sortSubviews(_ compare: (NSView, NSView, UnsafeMutableRawPointer?) -> ComparisonResult, context: UnsafeMutableRawPointer?)
```

## Parameters

- `compare`: A pointer to the comparator function. This function must take as arguments two subviews to be ordered and contextual data (supplied in context which may be arbitrary data used to help in the comparison. The comparator function should return NSOrderedAscending if the first subview should be ordered lower, NSOrderedDescending if the second subview should be ordered lower, and NSOrderedSame if their ordering isn’t important.
- `context`: Arbitrary data that might help the comparator function compare in its decisions.

## See Also

### Related Documentation

- [sortedArray(_:context:)](foundation/nsarray/sortedarray(_:context:).md)

### Adding and Removing Subviews

- [addSubview(_:)](appkit/nsview/addsubview(_:).md)
- [addSubview(_:positioned:relativeTo:)](appkit/nsview/addsubview(_:positioned:relativeto:).md)
- [removeFromSuperview()](appkit/nsview/removefromsuperview().md)
- [removeFromSuperviewWithoutNeedingDisplay()](appkit/nsview/removefromsuperviewwithoutneedingdisplay().md)
- [replaceSubview(_:with:)](appkit/nsview/replacesubview(_:with:).md)
