Hot answers tagged

37 votes
Accepted

How to sort colors properly?

If you're looking for a way to sort the colors in such a way as to make them seem the least discontinuous, then one way to think of it is that each color is a point in a space endowed with a distance ...
user avatar
25 votes
Accepted

Difference between Sort and SortBy

In general SortBy can do pretty much anything that Sort does; in some cases, possibly better or faster. You can find many comparisons on this site if you just search for both function names. I also ...
user avatar
  • 60.4k
22 votes
Accepted

Reproducing a ranking function that behaves like Excel's RANK

...
user avatar
  • 10.5k
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 ...
user avatar
  • 17.9k
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 ...
user avatar
  • 225k
20 votes

Rearrange the list in a specific way

A bit simpler: Permute[lst, SparseArray[order]] Example: ...
user avatar
  • 7,666
18 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: ...
user avatar
  • 45.5k
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 ...
user avatar
17 votes

How to sort colors properly?

Just for fun, how does a 3D Hilbert curve sample the 3D colourspace of RGB? and can it be used to sort colours? HilbertCurve3D[n] generates a 3D Hilbert curve of ...
user avatar
  • 14.9k
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, ...
user avatar
  • 124k
16 votes
Accepted

ls Ordering[Ordering[list]] optimal?

No, Ordering[Ordering[list]] not optimal. And yes, there is a faster method: ...
user avatar
15 votes
Accepted

How to get the ranked order

Ordering @ Ordering @ data {3,1,2} Also: ...
user avatar
  • 349k
15 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 ...
user avatar
  • 16k
15 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, ...
user avatar
  • 124k
14 votes

Sorting a list of variables by the numerical value they represent

Here's another way you might approach this kind of thing -- instead of assigning values to the variables using =, you can make them into rules. For example, here is ...
user avatar
  • 65.7k
14 votes

How to sort list of mixed types of elements with each type a specified canonical order?

...
user avatar
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: ...
user avatar
  • 264k
13 votes
Accepted

Unexpected behavior when using SortBy as a dataset filter

I do not believe that this behaviour is a bug. The correct usage would be SortBy[#age&] or SortBy[Key@"age"]. The rest of ...
user avatar
  • 67k
13 votes
Accepted

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

To keep the original order, use Cases[] with Alternatives: ...
user avatar
  • 493
12 votes
Accepted

Sorting Lists of lists of arbitrary length

To sort by a specific element use a pure function with the number in question. For your case (the second element) just do: SortBy[a, #[[2]] &] ...
user avatar
  • 32.5k
12 votes
Accepted

Lexicographic ordering of lists-of-lists?

This is due to Sort on lists sorting by the size of the sub-list first, and only applying lexicographic sort for equal-size lists. This is in fact documented. Based on this observation, here is one ...
user avatar
12 votes

How to sort colors properly?

I've decided to follow through on my suggestion in a comment to Kenny's answer to use Morton ordering (a.k.a. Z-ordering) of the colors in RGB space. Here's a short routine to generate the ...
user avatar
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 *) ...
user avatar
  • 1,216
11 votes

Unexpected behavior when using SortBy as a dataset filter

You have to use a pure function also for SortBy titanic[Select[#age > 65 &] /* SortBy[#age &]] Since ...
user avatar
  • 27.1k
11 votes
Accepted

Sorting a list of variables by the numerical value they represent

Unless you use := for varList definition. a,b... are lost. Here's more about that, in ...
user avatar
  • 132k
11 votes
Accepted

Wolfram Language symbols by their Ranks

This gives 5 symbols with the highest rank in "All": ...
user avatar
  • 126
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. ...
user avatar
  • 6,898
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 ...
user avatar
  • 225k
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 ...
user avatar
  • 25.1k
10 votes
Accepted

Import multiple files from folder in order of date added

Likely something like the following: Import[#, "PNG"] & /@ SortBy[FileNames[importString], FileDate[#,"Creation"]&] although I suspect there might be a ...
user avatar
  • 10.6k

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