# Questions tagged [sorting]

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

371 questions
Filter by
Sorted by
Tagged with
276 views

### Sorting a list of "Low", "Medium", "High" into low, medium, high

I have a list with 10.000 entries containing "low", "medium" & "high" data. It is in random order. How can I sort that to low, medium high?
• 43
116 views

### Extracting the second largest and minimum values, and plot

How to extract the second lowest and the second largest values of each row, and plot them as upper and lower graphs? I try with TakeLargest (or ...
• 173
1 vote
49 views

### Sorted Tuples without Filtering

Say I have a list $L$ where the elements can be sorted into some canonical order. I want to use Tuples[L,m] but I only want the output lists to be sorted and ...
• 145
84 views

### Tuples which treats re-ordering as equivalent [duplicate]

I would like to define a new function myTuples, that behaves just like Tuples, except that it treats, as with ...
• 951
92 views

### Re-sorting higher-order ordinary differential equations

I have some higher-order ordinary differential equations. Firstly, I want to collect the terms with common factors together. Besides, I want to re-sort the ODE with the highest order term first. How ...
• 191
44 views

### How to make a poker sort function?

I am working on a Poker program. I have two hands in the form of two lists with five strings each. Each string is has a value of 2,3,4,5,6,7,8,9,T,J,Q,K,A concatenated to a suit of C,D, H, S. Here is ...
• 1,037
1 vote
50 views

### How to sort a list of strings in alphabetical order? [closed]

I have a list of strings: listOfStrings = {"dog", "zebra", "donkey", "anteater", "fish"}; How do I sort them in ...
1 vote
62 views

### How to sort a "+" sequence and get back a new sequence of "+" sorted? [closed]

How to sort a "+" sequence like 1+5+3+2+4 to get back 1+2+3+4+5 Same sorting problem with polynoms (but I am mainly ...
196 views

### How to order the coordinates generated by ToBoundaryMesh counterclockwise?

The following code works for polygons. A square 4x4 is tried here: ...
1 vote
84 views

### Sort polynomial by monomial degree [closed]

there is an equation : z = a^2 + a b + a a' + a a' a'' + a b' and i want to rearrange the equation by sorting the terms by number of variable multiplication within. If my English is not well, let me ...
• 61
196 views

### Sorting Cartesian coordinates

Here is the problem: I create a coordinate list as follows: ...
125 views

### Sorting pairs of approximate numbers

In an ideal world I would have: list1 = {{2, 0}, {2, -1}, {2, 0}, {2, -1}}; Sort[list1] {{2, -1}, {2, -1}, {2, 0}, {2, 0}} while in the real world I have: ...
• 181
319 views

...
• 2,451
34 views

### Arrange an expression based on variables' names

I have a problem of arranging an expression as shown in the following picture. The first expression exp is derived from other functions and equations. I want to get ...
• 663
1 vote
76 views

### Arrange an expression based on functions' names

I have a problem of arranging an expression as shown in the following picture. The first expression disorderF is derived from other functions and equations, which ...
• 663
117 views

### For a given set, how to get rid of the some particular set?

Consider the following decomposition list of $\{a1,a2,a3,a4\}$ into three parts as $t$, t={{{a1, a2}, {a3}, {a4}}, {{a1}, {a2, a3}, {a4}}, {{a1}, {a2}, {a3, a4}}, {{a1, a2}, {a4}, {a3}}, {{a1}, {a2, ...
• 335
126 views

### Sorting colors in the plotstyle versus the grade of details [closed]

We have multi plots in a figure that are shown individually with specific colors. ...
• 4,647
192 views

### How to manipulate the colours when joining points in ListPlot

Suppose I plot out two sets of data on the same graph as follows: ListPlot[{{{1, 1}, {2, 2}, {4, 4}}, {{3, 3}, {5, 5}, {6, 6}}}] The colours of the points change ...
• 755
59 views

### Partially Orderless Arguments in Functions

I am a Mathematica beginner and I'm trying to implement a function of type f[i1_,i2_,i3_,i4_] such that it is orderless in the first two arguments and in the last ...
178 views

### How to sort lines into continous paths efficiently

As part of my work I get a bunch of lines from my co-worker. (Background: I do electromagnetic designs, my co-worker does the mechanical design). These lines are in random order, but I need them in ...
• 923
161 views

### Conditional sorting of columns in a matrix

I have a matrix with ten columns, and I want to rearrange these columns with three conditions. Column 1+2n and 2+2n (0<=n=<4) always remain together and in the same order (1+2n before 2+2n). ...
• 973
63 views

### How do you sort a list based on conditions? [closed]

If i have an list that ranges from 1 - 100. How can i sort elements from the list. For example i want elements that has greatest common divisor 1 with the number 24.
434 views

### How to sort first two numbers in sublists

Given t7 = t8 = {{3, 5, 1}, {19, 2, 9}, {4, 3, 1}, {19, 0, 8}}; I want to Sort the first two numbers in each sublist and keep ...
• 2,020
1 vote
72 views

### Rearrange a matrix with new Table Headings-List

I have used a procedure to rearrange my data in a more appropriate way for data analysis such that more similar elements lie next to each other and dissimilar ones far apart. Let's say I have a 5x5 ...
• 53
59 views

### How to sort arguments in descending order in an orderless function

If I define an operator to be orderless SetAttributes[CirclePlus,Orderless] It puts the arguments into canonical sorting, i.e. ...
• 844
67 views

### Does default 'Sort' generate always the same result? [closed]

Does default 'Sort' output always the same result regardless if we change its order? Suppose that L = {E1,E2,E3,E4,E5} (*here E1,..,E5 means any possible ...
• 2,451
156 views

Heres an MWE ...
• 707
385 views

### How to use Intersection[] but keep the original list order

I want to keep the original order of l1: ...
63 views

### Making function $S$ such that inputs as an list $\{1,3,2,4, \}$ and produces $s_{1234}$

I want to make $S[\{,\cdots, \}]$ as follows First input of $S$ is given list $\{1,2,3,\cdots, n\}$ and it produces $s_{123\cdots n}$ Further, if the ordering of the list is given differently, still ...
• 335
91 views

### Swapping items in a table

I have a code that generates various tables like ...
• 295
85 views

### Undesired side effect of SortBy [duplicate]

After long searching, I noticed that the SortBy[list,f] function has an annoying side effect: If the function f yields the same value for consecutive elements of the list, those are still sorted using ...
• 1,196
57 views

### How to obtain the grouped separate line curves from the disorder data?

The sample point data and codes are listed as following. The point-image tells us that the data can be divided into nine groups. However, why are they divided into two groups by ...
• 663
65 views

### Defining elements of matrix as a function of two variables (the position $(i,j)$ ) [closed]

I have the following array, which generated from grayscale image data,:  \left( \begin{array}{cccccccc} 162 & 162 & 162 & 161 & 162 & 157 & 163 & 161 \\ 162 & 162 &...
• 39
327 views

### On the ordering of the vertices of a polygon

Writing: pts = {{1, 0}, {2, 0}, {2, 3}, {3, 3}, {3, 4}, {0, 4}, {0, 3}, {1, 3}, {1, 0}}; Graphics[{Red, Point[pts], Blue, Line[pts]}] we get: and all of this ...
• 181
1 vote
47 views

### Mismatch for Ordering[list,1] and Position[list, Min[list]] [closed]

I am using Ordering[list, 1] to get the position of the minimum of a list, but in this example ...
• 528
178 views

### Combine SortBy with ReverseSortBy

Suppose I have a dataset, which I want to sort according to two criteria. However, I want the first criteria to be sorted ascending, and the second descending. This would require to somehow combine ...
• 2,470
103 views

### how to sum a column after SortBy[...]

Given a dataset: ...
• 4,391
1 vote
50 views

• 1,950
179 views

### Stop Canonical Ordering of Output List

I'm working with the code below, which works well for my purposes; however, instead of the canonical ordering seen in the output, I want the output produced in the order I give in the Cases line of ...
• 1,039
1 vote
65 views

...
• 155
285 views

### How to sort and extract a list containing products

I've some difficulties with lists containing products as elements, e.g. list = {A B A, A K B, A K R}, where A and B are from the same 'family', let's call it sector ...
97 views

### How do I ensure the substitution happens after I take the first derivative before taking my second derivative

The title may be a little confusing so let me explain what I mean: I am using lagrange brackets to produce some equations of motion for orbital mechanics, where the motion is split between oscillating ...
175 views

### How do I reorder and combine sublists based on sublist rankings given by another list?

I have two lists: ...
• 161
180 views

### How do I sort points {ai,bi}; i = 1,2,....,N so that immediate successors are closest?

Suppose there are points {ai,bi}; i = 1,....,N, but this is given in some jumbled order such as ...
• 1,398