Skip to main content
22 votes

Rearrange the list in a specific way

A bit simpler: Permute[lst, SparseArray[order]] Example: ...
Ray Shadow's user avatar
  • 7,846
22 votes
Accepted

Better way to sort course grades

This is a great use for the Association data structure, which makes so many tasks in Mathematica that much more pleasant. First, we can just write out a ranking ...
Pillsy's user avatar
  • 18.5k
22 votes
Accepted

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

This is not simplification. It is canonicalization. Bringing expressions to canonical form is very useful because then two expressions can be compared for equality by simply checking that they have ...
Szabolcs's user avatar
  • 236k
19 votes

Is there a "precedence table" for the canonical Sort ordering?

So I think the docs are mostly clear, if hard to visualize. Here's my version of such a table: ...
b3m2a1's user avatar
  • 46.9k
18 votes
Accepted

Quick QuickSort implementation

I also got angry about those randomly picked and ill-implemented benchmarks by the Julia team. I appreciate their efforts (jit compilers are useful), but the Fibonacci example was straight away ...
Henrik Schumacher's user avatar
17 votes

Terse Method to Swap Lowest for Highest?

How about: Module[{tmp = test}, With[{ord=Ordering[tmp]}, tmp[[ord]] = Reverse @ tmp[[ord]]]; tmp ] {56, 9, 4, 3, -5, -2, -3, 1, 2, 7, 60, 58, ...
Carl Woll's user avatar
  • 131k
16 votes
Accepted

ls Ordering[Ordering[list]] optimal?

No, Ordering[Ordering[list]] not optimal. And yes, there is a faster method: ...
Henrik Schumacher's user avatar
16 votes
Accepted

On the ordering of the vertices of a polygon

One way to solve the case provided is to apply a DistanceFunction to FindShortestTour. Here, I apply a stiff penalty to the next ...
Tim Laska's user avatar
  • 16.4k
16 votes
Accepted

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

To keep the original order, use Cases[] with Alternatives: ...
MiKK's user avatar
  • 563
16 votes
Accepted

How to sort first two numbers in sublists

You can use SubsetMap for this: SubsetMap[Sort,{1,2}] /@ {{3,5,1}, {19,2,9}, {4,3,1}, {19,0,8}} {{3, 5, 1}, {2, 19, 9}, {3, 4, ...
Carl Woll's user avatar
  • 131k
14 votes

Rearrange the list in a specific way

By using assignment to parts. Update: now cleaner. fn[list_, r_] := Module[{n = list}, n[[Values @ r]] = n[[Keys @ r]]; n ] Test: ...
Mr.Wizard's user avatar
  • 272k
13 votes
Accepted

How can we compare two strings?

AlphabeticOrder can compare strings alphabetically (or by other conventions used in dictionaries by various languages).
Szabolcs's user avatar
  • 236k
12 votes

Lowest Magnitude Eigenvalues of Large Sparse Matrices

Use the Arnoldi method with shift-inversion: Eigenvalues[A, 3, Method -> {"Arnoldi", "Criteria" -> "Magnitude", "Shift" -> 0}] gives you the three ...
Roman's user avatar
  • 48.3k
12 votes
Accepted

Using < or <= make different results when sort

The pair {{d, 4}, {c, 4}} does violate the order #1[[2]] < #2[[2]]: 4 < 4 (* False *) ...
Shin Kim's user avatar
  • 1,281
12 votes

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

tl;dr You are looking for "ranks", i.e. indices into the sorted version of the list. Ordering yields indices into the original (unsorted) version of the ...
Szabolcs's user avatar
  • 236k
11 votes
Accepted

Is there a way to sort NSolve solution (roots) automatically?

OK, I've had this problem before myself. Here's a solution to a harder problem? Take your code to generate the roots list. ...
MikeY's user avatar
  • 7,173
11 votes

Better way to sort course grades

I had to deal with similar problems many times, and I usually do this with associations. We start by defining an exhaustive list of grades in our desired order. I am not that familiar with the ...
Szabolcs's user avatar
  • 236k
11 votes
Accepted

Sort a list by elements of another list

Permute[list2, FindPermutation[ list2[[All,1]] , list1[[All,1]] ] ] {{A, 4}, {B, 5}, {C, 1}}
MikeY's user avatar
  • 7,173
11 votes

List of tuples without duplicates & repeated values

The accepted answer will quickly blow up with arguments of more than trivial sizes. For example, with vals = {10, 20, 5, a, b, c} and ...
ciao's user avatar
  • 25.9k
10 votes

How to sort colors properly?

I will use FeatureSpacePlot to do this.Plot their in feature space colors = RandomColor[100]; This is original order ...
yode's user avatar
  • 26.8k
10 votes

FindCurvePath for lines (rather than points)

Using FindShortestTour with a custom distance function: ...
Simon Woods's user avatar
  • 85.1k
10 votes

OrderedQ with string arguments

I think you have a good question. It seems that Sort treats "a" and "A" equivalently, and then sorts the elements that are equivalent. Here is an example that ...
Carl Woll's user avatar
  • 131k
10 votes
Accepted

Ordering a list of integers

ClearAll[f] f = ReverseSortBy[Minus @* Abs] @* DeleteCases[0] f @ n {1, 3, -3, -3, 10, 12}
kglr's user avatar
  • 397k
10 votes
Accepted

Find elements of array with largest absolute value

You could use SparseArray to do this: ...
Carl Woll's user avatar
  • 131k
10 votes

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

One way to keep the order in l1 is this: ...
Somos's user avatar
  • 4,917
10 votes
Accepted

Extracting the second largest and minimum values, and plot

ListLinePlot[{ RankedMax[#, 2] & /@ data, RankedMin[#, 2] & /@ data }]
Henrik Schumacher's user avatar
10 votes
Accepted

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

SortBy[list, ToExpression[StringDrop[ToString[#], 1]] &] Sort also work but needs longer code compared to previous one: <...
azerbajdzan's user avatar
  • 19.5k
10 votes

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

Grid@Flatten[Partition[list, UpTo[n]], {{2}, {1}}] $$\begin{array}{ccc} 1 & 4 & 7 \\ 2 & 5 & 8 \\ 3 & 6 & \text{} \\ \end{array}$$ ...
Syed's user avatar
  • 55.6k
10 votes
Accepted

How to confirm two sets contain the same vectors in any order?

One way: Sort@v1==Sort@v2 (* True *)
user1066's user avatar
  • 20k
9 votes

FindCurvePath for lines (rather than points)

This approach generates the data into newdat. ...
Feyre's user avatar
  • 8,617

Only top scored, non community-wiki answers of a minimum length are eligible