# sort

sort overloads sort operator

### Syntax

```
[N,location] = sort(X)
```

### Examples

The following silly example defines a regression problem with the constraint that the sum of the largest value and the smallest value of the decision variable is 1. As usual, we add explicit bound constraints to improve the big-M reformulations.

```
A = randn(20,5);
b = randn(20,1)*20;
x = sdpvar(5,1);
e = b-A*x;
[s,location] = sort(x);
F = [s(1)+s(end) == 1, -100 <= x <= 100];
optimize(F,norm(e,1));
```

Note that we can solve this particular problem more efficiently by using **max** and **min** instead (still a nonconvex problem, but it leads to a model with 10 binary variables, compared to the sort model which leads to 25 binary variables.)

```
F = [max(x) + min(x) == 1, -100 <= x <= 100];
optimize(F,norm(e,1));
```

### Comments

The operator is implemented using a mixed-integer model. based on big-M approach, hence it is crucial that all involved variables have explicit bound constraints.

The integer model for the sort operator is extremely expensive. Sorting a variable of **n** variables requires **n^2** binary variables.

Before you use sort, make sure to first check if you can use the convex operators sumk or sumabsk.

## Leave a Comment