# Tag Info

### Rearrange the list in a specific way

A bit simpler: Permute[lst, SparseArray[order]] Example: ...
• 7,846
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 ...
• 18.5k
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 ...
• 236k

### 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: ...
• 46.9k
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 ...

### 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, ...
• 131k
Accepted

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

No, Ordering[Ordering[list]] not optimal. And yes, there is a faster method: ...
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 ...
• 16.4k
Accepted

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

To keep the original order, use Cases[] with Alternatives: ...
• 563
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, ...
• 131k

### 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: ...
• 272k
Accepted

### How can we compare two strings?

AlphabeticOrder can compare strings alphabetically (or by other conventions used in dictionaries by various languages).
• 236k

### 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 ...
• 48.3k
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 *) ...
• 1,281

### 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 ...
• 236k
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. ...
• 7,173

### 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 ...
• 236k
Accepted

### Sort a list by elements of another list

Permute[list2, FindPermutation[ list2[[All,1]] , list1[[All,1]] ] ] {{A, 4}, {B, 5}, {C, 1}}
• 7,173

### 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 ...
• 25.9k

### How to sort colors properly?

I will use FeatureSpacePlot to do this.Plot their in feature space colors = RandomColor[100]; This is original order ...
• 26.8k

### FindCurvePath for lines (rather than points)

Using FindShortestTour with a custom distance function: ...
• 85.1k

### 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 ...
• 131k
Accepted

### Ordering a list of integers

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

### Find elements of array with largest absolute value

You could use SparseArray to do this: ...
• 131k

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

One way to keep the order in l1 is this: ...
• 4,917
Accepted

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

ListLinePlot[{ RankedMax[#, 2] & /@ data, RankedMin[#, 2] & /@ data }]
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: <...
• 19.5k

### 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}$$ ...
• 55.6k
Accepted

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

One way: Sort@v1==Sort@v2 (* True *)
• 20k

### FindCurvePath for lines (rather than points)

This approach generates the data into newdat. ...
• 8,617

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