7
$\begingroup$

I have two lists $R$ and $B$ of numbers written in red and blue. I would like to join those two lists and reorder them in increasing order to get a visual feeling of how both sequences are distributed. The problem is that when I try to use the Sort function it orders separately the red and the blue parts of the joined list. Is there a way to make Mathematica ignore the coloring?

R = Flatten[Table[Style[4 Pi^2 (k1^2 + k2^2), FontColor -> Red], {k1, -5, 
 5}, {k2, -5, 5}]];
B = Flatten[Table[Table[Style[4 Pi (2 m + 1) Abs[k3], FontColor -> Blue], 
 Abs[k3]], {m, 1, 5}, {k3, -5, 5}]];
Sort[Join[R, B]]
$\endgroup$
8
$\begingroup$
RB = R~Join~B;
NumericalSort[RB]

enter image description here

An alternative, slightly longer, solution could be:

RB = R~Join~B;
order = Ordering[N@RB[[;;,1]]];
RB[[order]]

With the same output as above. This can be convenient when the style of the two lists is different (as in comment below, one might be Bold while the other isn't): slicing the combined list when finding the order (i.e. doing order = Ordering[N@RB[[;;,1]]]; rather than order = Ordering[N@RB];) allows to ignore the differences in style between the two lists and only look at their numerical values.

$\endgroup$
  • 3
    $\begingroup$ It might be worth noting that the issue has nothing to do with the styling of the numbers (in this case at least) - the issue is really that Sort@{2 Pi, 3 Pi^2, 100 Pi} returns {2 Pi, 100 Pi, 3 Pi^2} - your solutions work because they convert the numbers into real numbers, which can be sorted properly (and not because they remove the styling, as you surely know). However, if I were to add e.g. Bold to one of the styles, your methods would unfortunately not work (which is of course not relevant in the specific case at hand) $\endgroup$ – Lukas Lang Nov 25 '19 at 11:08
  • $\begingroup$ @Lukas Lang absolutely true!!! my original solution used order = Ordering[N@RB[[;;,1]]] to strip off the style from the value, but then I realised there was no need in this specific case :) thx for pointing out this important bit of info $\endgroup$ – Fraccalo Nov 25 '19 at 11:26
  • $\begingroup$ actually, as it's really a good point, I'll put it back in my original answer $\endgroup$ – Fraccalo Nov 25 '19 at 11:29
  • 1
    $\begingroup$ For other users it should be noted that NumericalSort is relatively new, i.e., introduced in v11.1; SortBy was introduced in v6.0 (updated in v12.0) $\endgroup$ – Bob Hanlon Nov 25 '19 at 15:10
6
$\begingroup$

The issue is not styling, but the fact that the sort order used by Sort does not correspond to numerical ordering here:

Sort[{10 Pi, Pi^2}]          (* Out: {10 π, π^2} *)

You can explicitly sort by numerical value using SortBy:

SortBy[{10 Pi, Pi^2}, N]     (* Out: {π^2, 10 π} *)

So in your case:

SortBy[Join[R, B], N]

a picture of the appropriately sorted list

$\endgroup$
  • $\begingroup$ the problem is also styling in case the style of the two lists is different: try your code by putting ,Bold in ONLY one of the two lists, and you'll see it won't work anymore :) If you agree, maybe edit your post where you say that the issue is not styling because it could be confusing (in the sense that it's not styling in the exact OP, but it could be styling in a highly similar scenario) $\endgroup$ – Fraccalo Nov 25 '19 at 19:56

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.