Questions tagged [sorting]

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

Filter by
Sorted by
Tagged with
4 votes
1 answer
69 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, ...
user avatar
  • 143
2 votes
1 answer
84 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 ...
user avatar
  • 367
0 votes
1 answer
70 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 $\...
user avatar
  • 367
8 votes
1 answer
146 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 ...
user avatar
  • 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 ...
user avatar
  • 101
2 votes
1 answer
63 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? ...
user avatar
  • 525
4 votes
2 answers
291 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?
user avatar
  • 43
4 votes
2 answers
117 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 ...
user avatar
  • 173
1 vote
1 answer
55 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 ...
user avatar
4 votes
2 answers
85 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 ...
user avatar
  • 1,227
3 votes
1 answer
105 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 ...
user avatar
0 votes
0 answers
45 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 ...
user avatar
1 vote
1 answer
54 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 ...
user avatar
1 vote
1 answer
64 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 ...
user avatar
6 votes
4 answers
201 views

How to order the coordinates generated by ToBoundaryMesh counterclockwise?

The following code works for polygons. A square 4x4 is tried here: ...
user avatar
1 vote
1 answer
94 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 ...
user avatar
  • 61
5 votes
2 answers
214 views

Sorting Cartesian coordinates

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

Using < or <= make different results when sort

...
user avatar
  • 3,085
2 votes
1 answer
35 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 ...
user avatar
  • 663
1 vote
2 answers
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 ...
user avatar
  • 663
2 votes
2 answers
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, ...
user avatar
  • 367
5 votes
1 answer
129 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. ...
user avatar
  • 4,647
5 votes
3 answers
99 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 ...
user avatar
  • 131
9 votes
1 answer
179 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 ...
user avatar
  • 923
5 votes
1 answer
166 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). ...
user avatar
  • 973
0 votes
2 answers
67 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.
user avatar
5 votes
3 answers
439 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 ...
user avatar
  • 2,020
1 vote
2 answers
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 ...
user avatar
  • 53
2 votes
1 answer
62 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. ...
user avatar
  • 1,121
0 votes
1 answer
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 ...
user avatar
  • 3,085
5 votes
2 answers
159 views

"Ordering " command gives different order when n=1 vs n=2?

Heres an MWE ...
user avatar
10 votes
4 answers
404 views

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

I want to keep the original order of l1: ...
user avatar
  • 493
3 votes
4 answers
64 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 ...
user avatar
  • 367
2 votes
3 answers
91 views

Swapping items in a table

I have a code that generates various tables like ...
user avatar
  • 655
6 votes
1 answer
86 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 ...
user avatar
  • 1,196
0 votes
1 answer
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 ...
user avatar
  • 663
0 votes
1 answer
67 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 &...
user avatar
  • 39
10 votes
1 answer
350 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 ...
user avatar
  • 191
1 vote
0 answers
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 ...
user avatar
  • 528
6 votes
3 answers
182 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 ...
user avatar
4 votes
1 answer
103 views

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

Given a dataset: ...
user avatar
  • 4,401
1 vote
1 answer
50 views

l,r-operations on words $[[[1,2],3],4] \cdots, n]$, $[1,[2,[3,\cdots, [n-1,n]\cdots ]]]$

Suppose I have a word $\{1,2\}$, then $l(\{1,2\}) = \{[1,2]\} = \{1,2\} - \{2,1\}:=12-21$ and for the word with length $3$, (I will omit bracket), the $l$-operation is defined as $ l(1,2,3) = [[1,2],3]...
user avatar
  • 367
4 votes
1 answer
86 views

Concise way to do lexicographic ordering

I want to order a bunch of number sequences lexicographically. Did this with ...
user avatar
2 votes
1 answer
26 views

ordered set inclusion 2

Slightly change the previous question, ordered set inclusion, Now, consider $A=\{2,3\}$ and $B=\{2,3,4,5\}, C=\{5,2,4,3\}$, Now I want to make element $\{2,3\}$ is in $B$ as well as $C$, what can be ...
user avatar
  • 367
2 votes
2 answers
80 views

How to reorder {x,y} list data and plot as ListLinePlot

How to re-arrange data to find line plot in order (I tried with given below code). ...
user avatar
1 vote
1 answer
57 views

Sorting a Dataset by an enumerated column with assigned values

A $\mathtt{Dataset}$ has a column $C$ with enumerated values $c_i$, to each of which a number $n_{c_i}$ is manually assigned. The $\mathtt{Dataset}$ needs to be sorted based on the numbers. MWE: ...
user avatar
  • 1,980
6 votes
2 answers
181 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 ...
user avatar
  • 1,061
1 vote
0 answers
65 views

How can I sort in increasing order? [closed]

...
user avatar
  • 155
8 votes
6 answers
288 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 ...
user avatar

1
2 3 4 5
8