Questions tagged [sorting]

Questions on the sorting/ordering functions of Mathematica (Sort, SortBy, Ordering, etc.).

Filter by
Sorted by
Tagged with
51 votes
7 answers
4k views

Character edge finding

The following line of code finds the edge of a character: ...
user avatar
40 votes
4 answers
11k views

Sort lists according to the order of another

I have three parallel lists (i.e. the elements in position i of each list are related). I want to sort the first list using the function Sort and make the same ...
user avatar
35 votes
4 answers
18k views

How to sort colors properly?

I can't seem to be able to sort colors by their hue and perceptual brightness correctly. Here's my current solution, but it's not right; there are all these dark bands: ...
user avatar
  • 30.2k
33 votes
3 answers
2k views

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

This answer in which I wrote about the Operator Precedence Table was unexpectedly popular. That got me thinking about similar things, and I am wondering: Does an "ordering table" exist for the ...
user avatar
  • 264k
32 votes
5 answers
3k views

How do I reassign canonical ordering of symbols?

I have a big polynomial that evaluates to: $$A^2 e^2 \phi ^- \phi ^++A e \phi ^- \phi ^+ c_{2 w} g_Z+\frac{1}{2} A e g h W^- \phi ^+ +\ll13\gg,$$ which is supposed to represent some terms in the ...
user avatar
29 votes
5 answers
7k views

Sort data after specific ordering (ascending/descending) in multiple columns

I have some data: ...
user avatar
  • 3,600
28 votes
4 answers
3k views

Sort strings by natural ordering

Is there an easy way to sort strings according to their natural ordering? For example a list of file names ...
user avatar
  • 383
27 votes
2 answers
6k views

Sorting a list with secondary criterion

Is there an easy way to sort a list on multiple levels of criteria? By this I mean, first the list should be sorted according by criteria A (the same as using the usual sort function, ...
user avatar
26 votes
3 answers
2k views

Ordering problem

Let's look at the following example. Ordering[{0, 20 Sqrt[5], 40 Sqrt[5], 20 Sqrt[5], 20 Sqrt[10]} // N] gives the right answer ...
user avatar
  • 14.2k
23 votes
11 answers
3k views

Generating an ordered list of pairs of elements from ordered lists

I have a pair of ordered lists. I want to generate a new ordered list (using the same ordering) of length n by applying a binary operator to pairs of elements, one from each list, along with the index ...
user avatar
  • 2,622
22 votes
5 answers
518 views

Unexpected result {".a", "co", ".m"} from Sort[{".m", ".a", "co"}]

I came across the following situation: Evaluating Sort[{".m", ".a", "co"}] Results in {".a", "co", ".m"} I wondering: ...
user avatar
17 votes
7 answers
5k views

How to find rows that have maximum value?

Suppose if I have following list { {10,b,30}, {100,a,40}, {1000,b,10}, {1000,b,70}, {100,b,20}, {10,b,70} } How to find rows that have max value in ...
user avatar
17 votes
3 answers
555 views

Retaining and reusing a one-to-one mapping from a sort

Imagine I sort the elements of an array $Q$ by some means. Perhaps I sort strictly by the elements in the first column, perhaps I use SortBy[...] to sequentially sort by the elements in successive ...
user avatar
  • 1,417
17 votes
1 answer
394 views

Ordering function with recognition of duplicates

Fairly often I have a need to get the Ordering of an expression but with recognition of duplicates. For example: ...
user avatar
  • 264k
16 votes
4 answers
920 views

Reproducing a ranking function that behaves like Excel's RANK

I tried to find a function or expression in Mathematica that produces the same output as the RANK function in Excel (see its description here), but unfortunately I ...
user avatar
  • 507
16 votes
4 answers
646 views

FindCurvePath for lines (rather than points)

I have need of a function to find a good ordering for a series of lines, as FindCurvePath does for points. Sample data: ...
user avatar
  • 264k
16 votes
1 answer
908 views

How can I collect data for visualization of quick sort?

I know how to implement quick sort in Mathematica. But I don't know how to get the temp data for visualization, because I used recursion. I want the effect similar to this: ...
user avatar
  • 13.3k
16 votes
1 answer
496 views

ls Ordering[Ordering[list]] optimal?

Given a list list with unique elements, the task is to replace each element by its position in Sort[list]. For example, ...
user avatar
  • 35.5k
15 votes
6 answers
2k views

Rearrange the list in a specific way

Suppose I have a list like {"e", "c", "a", "d", "b"} and list of rules {1 -> 5, 2 -> 3, 3 -> 1, 4 -> 4, 5 -> 2}. ...
user avatar
15 votes
6 answers
887 views

Better way to sort course grades

I use the following code to create a histogram of grades. Grades are provided as a List of strings such as "A-", "D+", "E" (don't ask), etc. The +/- substitution hack is necessary to sort the grades ...
user avatar
15 votes
4 answers
534 views

How can we implement "Sleep Sort"?

Inspired by Implement Sleep Sort: Sleep Sort is an integer sorting algorithm I found on the Internet. It opens an output stream, and for each input numbers in parallel, delay for the number seconds ...
user avatar
  • 264k
15 votes
2 answers
3k views

Simple algorithm to find cycles in edge list

I have the edge list of an undirected graph which consists of disjoint "cycles" only. Example: {{1, 2}, {2, 3}, {3, 4}, {4, 1}, {5, 6}, {6, 7}, {7, 5}} Each ...
user avatar
  • 224k
15 votes
2 answers
697 views

Fastest way to combine pre-sorted lists?

I need to combine two long, already-sorted lists into one sorted list. My effort below takes advantage of the fact that the lists are already sorted; but it's far slower than just using Join and Sort,...
user avatar
  • 4,331
15 votes
1 answer
1k views

Lexicographic ordering of lists-of-lists?

I was surprised to discover that Mathematica does not sort lists-of-lists (LLs) lexicographically by default. For example, applying Sort to ...
user avatar
  • 11.4k
15 votes
1 answer
311 views

Dataset is 20X slow when adding column heads

I'm trying to use Dataset to explore some large data. I notice that the performance decrease more than 20X by adding heads to the columns. Here is an example: We ...
user avatar
14 votes
4 answers
2k views

Difference between Sort and SortBy

I was playing with Sort. All examples I have come across so far, i.e Sort[{1, -1, 3, -3, 2, 5}, Abs[#1]<Abs[#2]&] can expressed in a shorter fashion ...
user avatar
  • 207
14 votes
3 answers
371 views

Terse Method to Swap Lowest for Highest?

I have built a solution to swap the lowest values with the highest values in a list. With ...
user avatar
  • 39.2k
14 votes
1 answer
492 views

Quick QuickSort implementation

I am playing with julialang and loving it, but at the same time noticed that they have a benchmark comparison with Mathematica. I have already submited a better version of ...
user avatar
  • 1,599
14 votes
3 answers
284 views

Ordering Problem With VoronoiMesh

Imagine I want to model 2D cells moving in a tissue. Consider, as an example, Lloyd's relaxation algorithm. Using VoronoiMesh I'm able to mimic some cell movement. ...
user avatar
  • 4,138
13 votes
5 answers
4k views

Sort date list in format DD/MM/YYYY

I have a list of dates (ranging from 2007 to 2012) in the DD/MM/YYYY format and I need to sort this list. My problem is, when I simply use Sort[] I get: ...
user avatar
  • 3,248
13 votes
6 answers
944 views

Sort+Union on a list

Probably this is too trivial question for this forum but for some reason I am not getting it on my Mathematica 7 so far. I have a list ...
user avatar
  • 1,159
13 votes
1 answer
263 views

OrderedQ with string arguments

I don't find these results consistent: OrderedQ[{"a", "A"}] True ...
user avatar
  • 16.9k
12 votes
2 answers
2k views

Efficiently searching a list for the position of the closest number to a specified number

Searching by closest and position I wasn't able to find an answer—but found Nearest by guessing. Nearest, however, returns ...
user avatar
12 votes
1 answer
190 views

stable sort for association?

Should I be able to use the "list of functions" approach to getting a stable sort of an association? In 11.1 I see, ...
user avatar
  • 12.3k
11 votes
4 answers
953 views

Gather list elements by labels

The following list has some elements that are labeled. For example {1, 2} -> 1, {-1, 3} -> 3, etc: ...
user avatar
  • 2,405
11 votes
5 answers
870 views

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

For example, here is a list {{1, "u"}, {6, "d"}, {3, "u"}, {4, "d"}, {2, "u"}, {5, "u"}, {3, "d"}, {1, "d"}, {4, "u"}, {2, "d"}, {5, "d"}, {6, "u"}} The ...
user avatar
  • 15.6k
11 votes
1 answer
1k views

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

As I understand it, Mathematica often uses alphabetical order when simplifying expressions. For example, b-a simplifies to -a+b. ...
user avatar
  • 307
11 votes
3 answers
649 views

Rearranging a List

I am trying to rearrange/sort a list i.e. {{1,2},{10,140},{43,10},{1,140},{43,2}} into ...
user avatar
  • 3,244
11 votes
2 answers
467 views

SortBy misbehaving?

It maybe a stupid thing in the end but I'm stuck a couple of hours now. I have a list of 2 points on the plane and I want to get the one with the biggest second coordinate. I thought I knew how ...
user avatar
  • 933
11 votes
1 answer
362 views

How to pass composite function list to SortBy?

In order to sort alphanumeric-as-string data of the form {"T3", "T14", "T1", "E2"}, so that "T14" comes after ...
user avatar
  • 14.8k
11 votes
1 answer
564 views

Ranking Function Allowing for Ties

I have a list that I need to be able to assign ranks to individual elements in, giving it a number related to its position if the list were to be sorted, allowing for ties. For example, ...
user avatar
  • 111
11 votes
1 answer
1k views

Sorting function for non commuting bosons

I am trying to write a sorting function which will sort expressions involving products of bosonic objects which do not commute. For example, I can have objects like $a,\ a^\dagger,\ b,\ b^\dagger$ ...
user avatar
  • 495
11 votes
1 answer
542 views

Max element of a list with a custom ordering function

I need a function to find max element of a list according to some custom ordering function, assuming that the function implements a total order (elements being compared might not even be numbers). ...
user avatar
10 votes
2 answers
579 views

How to generate Intervals from a given list?

If I have a list list={3,7,4,5,1} how to get the List of Intervals /Ranges result = {{1,3},{3,4},{4,5},{5,7}}
user avatar
10 votes
4 answers
384 views

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

I want to keep the original order of l1: ...
user avatar
10 votes
2 answers
679 views

Unexpected behavior when using SortBy as a dataset filter

I am still experimenting with datasets. Recently I tried to query the Titanic example to get info on the oldest passengers. Here is what I tried ...
user avatar
  • 106k
10 votes
3 answers
604 views

Sorting Lists of lists of arbitrary length

I'm trying to sort a list of lists. The sublists are defined in length, but the length of the list containing it varies. A simplified set might be: ...
user avatar
  • 387
10 votes
3 answers
497 views

Wolfram Language symbols by their Ranks

I'd like to get the top 50, the bottom 50 and an ordered list of all Wolfram Language symbols based on their "Ranks". With ...
user avatar
  • 27.1k
10 votes
1 answer
327 views

On the ordering of the vertices of a polygon

Writing: pts = {{1, 0}, {2, 0}, {2, 3}, {3, 3}, {3, 4}, {0, 4}, {0, 3}, {1, 3}, {1, 0}}; Graphics[{Red, Point[pts], Blue, Line[pts]}] we get: and all of this ...
user avatar
  • 181
10 votes
1 answer
251 views

How to get the ranked order

data = {33, 6, 10}; Ordering[data] (* {2, 3, 1} *) What I really want is, 3 for 33, as it is the 3rd, if sorted; 1 for 6, as it is the 1st, if sorted; and 2 for ...
user avatar

1
2 3 4 5
8