# Questions tagged [sorting]

Questions on the sorting/ordering functions of Mathematica (Sort, SortBy, Ordering, etc.).

287 questions
Filter by
Sorted by
Tagged with
41 views

### SortBy without final cannonical ordering

How can I SortBy a list without final cannonical ordering? E.g. SortBy[{9, 1, 2, 5, 3, 7}, # Boole[# >= 6] & ] returns <...
139 views

### Fast way of re-ordering list

I have a list which at each iteration of my algorithm is being modified. What I want to do is to find the elements that satisfy certain criterion (below it's simply being above the neighborhood ...
142 views

### How can we compare two strings?

In Python, you can do this: "ab" > "ac" which returns False. In Mathematica, it doesn't evaluate. But why? These are ...
94 views

### How to group points based on mesh primitive membership

As a simple example of what I want to do, let's say I have a Voronoi mesh in a $10\times10$ area with $10$ random "seed points": ...
235 views

### SortBy first element then second element, and so on?

SortBy can sort list by first element easily like so: SortBy[{{1, 6}, {1, 2, 8}, {2}}, First] and the result is: {{1, 6}, {...
155 views

### How can I sort a nested list?

I have a nested list of the form: ...
31 views

### Pick out two related elements from a list using criterion

Suppose I have a list: $lst= \{\{k_1, g_1\},\{k_2,g_2\},...,\{k_{max},g_{max}\},...,\{k_n,g_n\}\}$ Here $\{k_{max},g_{max}\}$ is a special element, in which $g_{max}$ is the maximum of all $g_{i}$ ...
35 views

### Matching list of eigenvalues and eigenvectors and removing negative values [closed]

I Have a matrix M (14x14) dimension in terms of variable k. Due to the complexity I wish to diagonalize M numerically as I can't symbolically. ...
77 views

### How to separate and re-order a list of points?

Suppose that we have a list of points. By plotting them we find two curves but the points are in a sequence of first fixing $x$ and then increasing $y$, next fixing $x+dx$ and then increasing $y$ and ...
20 views

### Canonical order for expressions that applied in Sort? [duplicate]

Sort[list] sorts the elements of list into canonical order. As an example for sorting expressions: Sort[{y, x^2, x + y, y^3}] ...
66 views

### Sorting list on multiple criteria based on means of subgroups

I have a set of records where each record is for a particular item. An item is a member of a particular subgroup and a subgroup is a member of a particular group. One item is always a member of a ...
40 views

### How do I order the solution of Solve?

How can I order the solutions of Solve from the one with the smallest real part to the one with the greatest real part?
69 views

### How to sort Dataset where each row include a sequence of strings and integers

I got this type of data : ...
42 views

### How to SortBy Min[Last]

I have a list of the form { {1, {3., ∞, ∞, ∞}}, {2, {1.,1., ∞, ∞}}, {3, {0.333333, 0.666667, ∞, ∞}} } and I would like to sort it by the minimal of the nested ...
45 views

### Sort and extract values in a nested list

I want to sort the following nested list of peak points by the y values while keeping the dimensions consistent. I then want to extract the x value of the largest and second largest y values (if the ...
72 views

### How to sort by subscript

I notice that ...
80 views

### Sorting a Collection of Lists by Comparing Different Parts

I am looking for a way to use Sort[] to sort a collection of lists by comparing different parts of these lists. For example, sorting by comparing the second part of ...
111 views

I am trying to understand how to sort by multiple columns in Mathematica. Here is a simple data set consisting of results of sports matches. Each element represents a game, and contains fields for ...
41 views

### Sorting a list according to the position of elements

I have a list of elements, which I want to sort according to the even/odd numbered positions of elements. Basically creating two lists out of one, respectively having elements placed at even/odd ...
499 views

### Find elements of array with largest absolute value

I am wondering how to do in Mathematica the following: Given as an input a large array, I want to output a list of top $n$ entries with largest absolute value + their location within the array. ...
66 views

### Sort Data Sets by Lines/Neighbours

I have several sets of data which together yield Fig. 1. As can be seen from the colours of the dots, the data sets are mixed, but do obviously belong to several distinct lines. My question is: How ...
52 views

### Using SortBy on a list of a list of a list

I have quite a typical physics problem at hand, I want the eigenvalues and -vectors of a Hamiltonian matrix. I have a parameter s, which I vary in 1000 steps and for each step I want my H matrix to be ...
180 views

### Sorting sequences independent of color

I have two lists $R$ and $B$ of numbers written in red and blue. I would like to join those two lists and reorder them in increasing order to get a visual feeling of how both sequences are distributed....
65 views

### Why do these commands give different answers?

I'm using v12 ...
172 views

### Canonical ordering of days of week

The entities representing the days of the week sort alphabetically: ...
85 views

### Sort Dataset by Row index after GroupBy

I would like to sort my Dataset by its row index. Unlike an ordinary column, this index does not have a name. ...
92 views

### Sort Associations by its Values (which are nested lists)

I have associations where the values are nested lists: ...
494 views

### Ordering a list of integers

I have a list of integers which I am trying to order in the following way: remove all zeros from the list sort in increasing order of absolute value put positive values before negative values I.e. ...
119 views

### Group symbols by a given partition of the occuring symbols

I have following expression $$-3 b_{\sigma _d}-3 b_{\sigma _v}-b-b_2+2 j-2 j_2-2 j_3+2 j_6+12$$ Mathematica input ...
82 views

### Sorting a list into “all possible ways” - sorting with incomplete information

Familiar example of sorting objects I have a list of $n$ "vectors" $\{x^{(1)},...,x^{(n)}\}$ of form $x^{(i)}=\sum_{j=1}^m c_j^{(i)}x_j,i=1\dots n$, where $c_j^{(i)}$ are nonnegative ...
279 views

### Derivative of Sort

I want to calculate gradient of Sort[{x1,x2,x3,x4}] but it seems like I'm getting a wrong answer. Specifically ...
119 views

### How to show just one function from a stored plot?

Q: Is there a general way to remove particular functions from a previously stored call to a plot function? Here is a specific example: ...
248 views

### Sorting a list according to some pre-specified rules

Given a list such as {12,9,3,42,30,10,11,1,2} I would like to place only the multiples of 3 in the natural order without changing the order of the other ...
213 views

### Sort a list of lists by increasing order of elements [closed]

What is the quickest way to sort a list of lists of integers, such that the first sorting criterion is the first element of the list, the next is the second element of the list and so on? E.g. The ...
93 views

### Sorting polynomials based on their y value and finding their intersection

I've got the following list of polynomials, ...
245 views

### Lowest Magnitude Eigenvalues of Large Sparse Matrices

I am trying to find the first three lowest eigenvalues of large sparse matrices of size range $10^3 - 10^5$. The matrices depend on some parameter $x$, so I first construct the matrices and then use ...
1k views

### Why does Sin[b-a] simplify to -Sin[a-b]?

As I understand it, Mathematica often uses alphabetical order when simplifying expressions. For example, b-a simplifies to -a+b. ...
43 views

### How to sort the “do” output?

So I have function (FindCoefficients) that iterates through some values to find the maximum value for a function. It is much quicker to hold the non-linear values constant and do a find maximum for ...
182 views

### Sorting upon the first element of a nested list and show that as numerical values

I wish to sort the below list upon the first elements: ...
90 views

### how to sort an expression with descending coefficients?

I have an expression for example psi = 0.24 FF1 FF2 FF3[-1] + 0.23 FF1 FF2 FF3 + 0.5 FF1 FF2 FF3 + 0.25 FF1 FF2 FF3 I wonder how ...
60 views

### Sorting each column in a nested list contained in an association

I have an Association which contains three arrays 200x1000x2. Each row m of a matrix denotes a different set of parameters, while each column n contains the outcomes of the function for a specific ...
86 views

### AlphabeticSort of Chinese character is not working now

I want to sort strings of chinese characters. I used to use below code list={"大家","早上","好"}; AlphabeticSort[list, Entity["Language", "ChineseMandarin"]] And it ...
447 views

### ls Ordering[Ordering[list]] optimal?

Given a list list with unique elements, the task is to replace each element by its position in Sort[list]. For example, ...
124 views

### How can I animate the sorting by Sort, BarChart and Animate? [duplicate]

I try to make an animation of sorting like this but something goes wrong. ...
486 views

### Sort a list by elements of another list

I know there are a plenty of other questions here which appear to be similar, however I did not found anything which could give me a hint. I have two lists: ...
366 views

### Terse Method to Swap Lowest for Highest?

I have built a solution to swap the lowest values with the highest values in a list. With ...
540 views

### Sort with assumptions

I have a list which looks like this list = {0, Subscript[x,7], -Subscript[x,3]-Subscript[x,9], -Subscript[x,9]}; and all the $x_i$'s are positive semidefinite (i....
48 views

### Inconsistency in SortBy [duplicate]

I have a list of data, I tried to SortBy it in two equivalent way, but the result is different. The data variable is something ...