22
votes
Rearrange the list in a specific way
A bit simpler:
Permute[lst, SparseArray[order]]
Example:
...
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 ...
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 ...
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:
...
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 ...
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, ...
17
votes
Accepted
ls Ordering[Ordering[list]] optimal?
No, Ordering[Ordering[list]] not optimal. And yes, there is a faster method:
...
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 ...
16
votes
Accepted
How to use Intersection[] but keep the original list order?
To keep the original order, use Cases[] with Alternatives:
...
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, ...
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:
...
13
votes
Accepted
How can we compare two strings?
AlphabeticOrder can compare strings alphabetically (or by other conventions used in dictionaries by various languages).
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 ...
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 *)
...
12
votes
Accepted
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 ...
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.
...
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 ...
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}}
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 ...
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
...
10
votes
FindCurvePath for lines (rather than points)
Using FindShortestTour with a custom distance function:
...
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 ...
10
votes
Accepted
Ordering a list of integers
ClearAll[f]
f = ReverseSortBy[Minus @* Abs] @* DeleteCases[0]
f @ n
{1, 3, -3, -3, 10, 12}
10
votes
Accepted
10
votes
How to use Intersection[] but keep the original list order?
One way to keep the order in l1 is this:
...
10
votes
Accepted
Extracting the second largest and minimum values, and plot
ListLinePlot[{
RankedMax[#, 2] & /@ data,
RankedMin[#, 2] & /@ data
}]
10
votes
Accepted
Sorting a list of "Low", "Medium", "High" into low, medium, high
The idea in the code below is to replace each entry in the list by a number corresponding to the "priority" of that element, and then sorting according to that priority using ...
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:
<...
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}$$
...
10
votes
Accepted
How to confirm two sets contain the same vectors in any order?
One way:
Sort@v1==Sort@v2
(* True *)
Only top scored, non community-wiki answers of a minimum length are eligible
Related Tags
sorting × 439list-manipulation × 172
matrix × 35
order × 27
functions × 23
performance-tuning × 23
plotting × 20
string-manipulation × 14
formatting × 14
dataset × 11
polynomials × 10
associations × 10
import × 9
filtering × 9
function-construction × 8
graphs-and-networks × 8
linear-algebra × 8
gathering × 8
programming × 7
eigenvalues × 7
permutation × 7
syntax × 6
data × 6
files-and-directories × 6
code-request × 6