<commit_before>---
layout: single
category: command
author_profile: false
excerpt: ""
title: sort
tags:
comments: true
date: '2016-09-17'
sidebar:
  nav: "commands"
---

Images:mini-category.gif  [!Mixed-integer based nonlinear operators]

### Syntax

````matlab
[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 [Tutorials.Big-M big-M] reformulations.
````matlabb
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.)
````matlabb
F = [max(x) + min(x) == 1, -100 <= x <= 100];
optimize(F,norm(e,1));
````


### Comments
Since [sort] is implemented using a [Tutorials.Big-M big-M] approach, 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 [Tutorials.GraphRepresentations#operators sumk] or [Tutorials.GraphRepresentations#operators sumabsk].<commit_msg>Update 2017-09-17-sort.md<commit_after>---
layout: single
category: command
author_profile: false
excerpt: ""
title: sort
tags: [Logic programming]
comments: true
date: '2016-09-17'
sidebar:
  nav: "commands"
---



### Syntax

````matlab
[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 [Tutorials.Big-M big-M] reformulations.

````matlab
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.)

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


### Comments

Since [sort] is implemented using a [big-M](/tutorial/bigmandconvexhulls) approach, 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].
{'fraction_non_alphanumeric': 0.1346027577150361, 'fraction_numerical': 0.02954694681549573, 'mean_word_length': 4.7927756653992395, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '649777', 'n_tokens_mistral': 1025, 'n_tokens_neox': 928, 'n_words': 392}