Questions tagged [sorting]

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

Filter by
Sorted by
Tagged with
5 votes
2 answers
137 views

Sorting a list by a single particular element in a specified order

I have a list of lists -- a list of sublists -- that I wish to sort in a specified order. The following function, sortBy2ndElement, sorts a list of lists (...
  • 9,747
3 votes
2 answers
85 views

Order elements of list according to predefined ordering [closed]

I have quite a lot of sublists of different length whose elements I would like to order according to a specific predefined ordering. To be explicit: I am given a specific ordering of indices (i,j,k,l,...
1 vote
2 answers
55 views

Is it possible to prevent two functions from commuting?

I have an expression of the following type: x f[a] g[b] where x is a number but f[a] and <...
  • 651
6 votes
1 answer
111 views

Sort the nested list by the sublist content; sublists of different lengths

I would like to write the function NewSort function that sorts a list of sorted sublists, basing first on the canonical ordering of subsequent sublist elements. In ...
3 votes
1 answer
71 views

Basic question Sort criteria

Can you explain the following codes? (output of codes) I know that there is the default ordering for any expression in Mathematica and Sort uses the default ...
  • 3,529
2 votes
1 answer
69 views

Cycle symmetric Sort for arguments of a function. Put trace in canonical order

I need a new Sort for the arguments of TR that maintains cyclicity, TR[a,b,c] = TR[b,c,a] = TR[c,a,b] cyclicSort[TR[b,a,c]] TR[a,c,b] ...
  • 630
7 votes
1 answer
216 views

Ordering Arguments of Antisymmetric Functions in Cyclic Way

I have two functions ang and sqa, each of which take two arguments which must be distinct integers between 1 and 6. For instance I could have ang[2,1] or ...
  • 137
3 votes
3 answers
67 views

Sort a list using a scoring list with Switch [duplicate]

I have a minimal working example (MWE) list called listToSort that is a list of string triplets: ...
  • 9,747
5 votes
1 answer
135 views

Fail to understand Ordering[], need to get the ranking of elements in a list

Ordering[{2, 1, 6, 4, 7, 10}] gives me as a result, {2, 1, 4, 3, 5, 6}, which I understand, 10 is the 6th smallest element. However ...
8 votes
2 answers
303 views

SortBy with a 2nd argument that specifies not to sort the list?

One can specify the second argument of SortBy not to sub-sort ties, i.e. cases that have identical values according to the main sorting function. ...
0 votes
1 answer
50 views

Sorting a list by increasing order algorythm [closed]

Im new to loops on mathematica,was wondering how could I create a function to loop through a list which finds the index of the smallest value of that list, makes a sublist from index 1 to that ...
6 votes
3 answers
300 views

How to Partition List into sublists so that it orders down columns when placed into a Grid instead of across rows

Partitioning a List into sub-lists and placing them directly into a Grid orders sub-lists across rows. Is there an idiot-proof built-in function or a simple ...
  • 2,337
1 vote
0 answers
32 views

How to create a Sort function that does not differentiate between String rule keys and Symbol rule keys

Im trying to Sort Rule-s according to Key-s (or names) but the normal ...
  • 2,337
3 votes
3 answers
211 views

Sorting a list based only on a certain property of the first element of sublists

Suppose I have a list called roster. Each element in roster is a list containing two strings: (i) the species of animal and (ii) ...
  • 9,747
0 votes
1 answer
41 views

Sorting a dataset on multiple column

Following an answer provided by kglr in a previous post: ...
  • 1,576
0 votes
1 answer
56 views

Sorting a symmetric matrix alphanumerically

NOT related to my related question! (Different problem source) This time, I want to sort a symmetric matrix M, and it must stay symmetric (i.e. row and col sort is the same). WLOG the diagonal is 0 (...
1 vote
0 answers
73 views

SortBy the second element in the sublists

Is there a way to sort sublists in a list by the sublists' second element? For example, if the list is: ...
  • 205
3 votes
2 answers
432 views

How can I sort {S1, S15, S19, S2, ...}?

How can I sort this in order from small to large? Simple Sort doesn't work here. ...
  • 2,822
4 votes
4 answers
179 views

Creating a cycle with conditions

I would like to arrange a gift exchange between 5 people, Alice, Bob, Carl, Danny, and Ellen, and I would like the gifts to be passed in a randomly created cycle (B->C->A->D->E->B), for ...
  • 782
4 votes
3 answers
296 views

Insert without extra brackets

I have a List1: {{1, -1.1}, {2, -1}, {3, -0.9}, {4, 0}, {10, -0.4}} and List2: {{2, -0.6}, {3, -0.6}} and I would like insert ...
  • 749
0 votes
0 answers
30 views

Using Insert for multiple Manipulations at once [duplicate]

I have a List1: {{1, -1.1}, {2, -1}, {3, -0.9}, {4, 0}, {5, -0.8}, {6, -0.8}, {7, -0.3}, {8, -0.1}, {9, -0.7}, {10, -0.4}} and List2: ...
  • 749
0 votes
1 answer
49 views

Sorting a list with certain conditions [closed]

I have this List1: ...
  • 749
4 votes
3 answers
296 views

How to replace the element in list of list by its ordering index

I have a list of list: res={{9, 1, 5}, {3, 6, 12}} I want to change it to res={{5, 1, 3}, {2, 4, 6}} by following their ordering ...
  • 95
9 votes
5 answers
333 views

Collect terms with the same denominator

I have an expression $$\frac{A}{x}+\frac{A_2}{x^2}-\frac{D_2}{x}+\frac{C_2}{x^2}++\frac{F_2}{x^3}+\frac{I_2}{x^2y}+\frac{U_2}{y^2x}+\frac{Z_3}{x^2y}+\frac{J_2}{y^2x}+\cdots$$ and I am looking for a ...
3 votes
4 answers
182 views

Finding relative ordering of two lists

Suppose you have two lists A and B which are related by a permutation, and have no repeated elements. Is there a simple and ...
3 votes
2 answers
81 views

Parting a table by second column elements

I have 2 X m table with elements {{x1, y1}, {x2, y2}, .... {xi, yi}, .....{xm, ym}}. It is an experimental data, which can be seen in the attached picture. There is a significant noise and ...
  • 1,103
6 votes
4 answers
594 views

Find min and max indices of highest and lowest elements in sorted list

I have a list of integers of the form ...
  • 229
1 vote
4 answers
198 views

Counting number of points in fluctuating lines that fall within a circle

I have a list of points pts that fluctuate around some mean line. pts is pre-sorted by the projection of the points onto this ...
  • 229
6 votes
1 answer
144 views

Fastest Way to Find nearest indices between two sorted lists

I have a sorted list of floats of length n and an n x n list of lists, where each nested ...
  • 229
4 votes
1 answer
80 views

Ordered list of integer vectors

For a given length $n$ and a maximum positive integer value $N$, I'd like to generate a list that contains all $n$-vectors with entries between $-N$ and $N$ (of which there are $(2N+1)^n$. Further, ...
  • 153
2 votes
1 answer
89 views

Dropping non-commutative product in the end

Suppose my expressions are given as a combinations of non-commutative products, i.e., $2 X**Y**Z + 3 Y**Z**X$, I want to make this as commutative product and get $5 X**Y**Z$. And want similar things ...
  • 437
0 votes
1 answer
75 views

substitution $A \mapsto A + \delta_A$ with some operator $\delta_A$

I want my input letter $A B$ and substitute $A \mapsto A + \delta_A$ and $B \mapsto B+ \delta_B$ so that \begin{align} A B + BA \mapsto A B + B A + \delta_A B + \delta_B A \end{align} Here $\...
  • 437
8 votes
1 answer
156 views

sorting vector elements of a list by certain rules

I want to write code that sorts out a list by some specific rules. The rules are logically straightforward, but I am having trouble coding it. This is my input list ...
  • 109
0 votes
0 answers
21 views

Collect terms unexpected behaviour with subscripts and different variable names

If I write Collect[x+a+1, x] the output I get is what I expect 1 + a + x constant terms with respect to ...
  • 101
4 votes
2 answers
131 views

Sorting of a list of lists - i.e. list of matrices - what is the order of the sort determined by?

In the documentation we have: Sort[list] sorts the elements of list into canonical order. Maybe I am missing something but what does "canonical order" mean for sorting a list of matrices? ...
  • 717
4 votes
2 answers
400 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
4 votes
2 answers
134 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
1 answer
60 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 ...
4 votes
2 answers
97 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 ...
  • 1,317
3 votes
1 answer
135 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 ...
1 vote
1 answer
169 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
1 answer
68 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 ...
6 votes
4 answers
205 views

How to order the coordinates generated by ToBoundaryMesh counterclockwise?

The following code works for polygons. A square 4x4 is tried here: ...
1 vote
1 answer
117 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
5 votes
2 answers
235 views

Sorting Cartesian coordinates

Here is the problem: I create a coordinate list as follows: ...
2 votes
2 answers
127 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: ...
  • 291
2 votes
1 answer
348 views

Using < or <= make different results when sort

...
  • 3,529
2 votes
1 answer
38 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
2 answers
78 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
2 votes
2 answers
119 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, ...
  • 437

1
2 3 4 5
9