Questions tagged [sorting]
Questions on the sorting/ordering functions of Mathematica (Sort, SortBy, Ordering, etc.).
81
questions
50
votes
7answers
4k views
17
votes
7answers
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 ...
25
votes
2answers
1k 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
...
40
votes
4answers
10k 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 ...
32
votes
5answers
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 ...
26
votes
4answers
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
...
7
votes
3answers
2k views
How to plot several functions without jumping? (multiple eigenvalues of a system as functions of 2 parameters)
I've been working on this research problem for months with some success by no global/proper solution to the problem.
So I have square, Hermitian matrices of various sizes ($8,20,38$ dimensions) with ...
7
votes
2answers
902 views
Position function not always returning an answer even with no apparent problems
I'm having some problems with Position.
Sometimes it will give an empty list instead of the actual position of the element I am looking for when that element is ...
30
votes
3answers
1k 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 ...
13
votes
6answers
923 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
...
17
votes
3answers
511 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 ...
17
votes
1answer
378 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:
...
29
votes
5answers
7k views
15
votes
4answers
592 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:
...
11
votes
4answers
915 views
Gather list elements by labels
The following list has some elements that are labeled. For example {1, 2} -> 1, {-1, 3} -> 3, etc:
...
10
votes
2answers
530 views
Complement on pre-sorted lists
Is there any way to instruct Complement to skip the sorting part? If the answer is no (likely), the next question would be: how can I remove from a sorted list ...
15
votes
2answers
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 ...
23
votes
11answers
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 ...
16
votes
1answer
489 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,
...
14
votes
4answers
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 ...
15
votes
1answer
863 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:
...
7
votes
4answers
858 views
Sorting a list of variables by the numerical value they represent
What would be the simplest method of sorting a list of variables according to the value they represent:
Let:
...
22
votes
5answers
506 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: ...
7
votes
1answer
281 views
How to get all possible orderings (equal ranks allowed) for given set of $N$ objects?
Hope there is a solution besides the tedious generation of nested loops. (Trying to avoid reinventing the wheel.)
Here is an example with $N = 3$ objects. There are $13$ needed orderings (first ...
27
votes
2answers
5k 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, ...
34
votes
4answers
15k 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:
...
11
votes
1answer
926 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$ ...
6
votes
6answers
488 views
Sort a list of coordinates by frequency from first component of each coordinate
The situation is that I have a List like this:
...
5
votes
1answer
729 views
Plotting eigenvalues smoothly
I am trying to plot how the eigenvalues of a matrix change by changing a given a parameter t. Of course the ordering of the eigenvalues does not matter mathematically but it does when I want to make ...
5
votes
8answers
628 views
How to sort my list?
For example I have two lists of such structure
L1 = {{"Australia",a1}, {"USA",a2}, {"Norway",a3},...}
L2 = {{"Russia",b1}, {"Norway",b2}, {"Japan",b3},...}
I ...
15
votes
4answers
521 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 ...
11
votes
1answer
349 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 ...
4
votes
3answers
580 views
Complement[] changes order of elements?
Consider this example:
Complement[{a, y, c, d, e}, {a, c}, {d}]
(*{e, y}*)
However, I was expecting the result to be:
...
4
votes
2answers
337 views
Sort by returning positions [duplicate]
How can I computationally efficiently sort a list so as to return the positions and not the actual values at those positions (which SortBy does)?
4
votes
4answers
773 views
How to pick out the element in a list of lists which satisfies a particular condition?
I have got a list:
...
15
votes
6answers
855 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 ...
10
votes
2answers
520 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}}
10
votes
2answers
654 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
...
3
votes
2answers
583 views
How can I sort the polynomial by the degree of $x$
f1 = a x^2 + b x + c + x^3
(*
a x^2+b x+c+x^3
*)
f1 /. x -> (x - a/3) // Expand
(*
(2 a^3)/...
3
votes
2answers
404 views
Output all steps of Sort [duplicate]
Is there a way you output each partial result of the Sort function? i.e.
Sort[{2,3,1}]
{2,1,3}
{1,2,3}
2
votes
3answers
443 views
Re-arranging / selecting of a data table very slow in For loop [duplicate]
My task is quite simple; I have a table of data like
M = {{1,2}, {1,3}, {3,4}, {5,1}, {6,9}, {6,8}, {6,1}};
so the table is sorted with respect to the first ...
0
votes
1answer
214 views
Problem with Sort - unexpected order with Sqrt [duplicate]
Sort[{13, Sqrt[157], Sqrt[163]}]
(*{13, Sqrt[157], Sqrt[157]}*)
Sort[{13, Sqrt[157], Sqrt[157]}, #1 < #2 &]
(*{Sqrt[157], Sqrt[157], 13}*)
This seems ...
15
votes
1answer
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 ...
15
votes
1answer
300 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 ...
15
votes
6answers
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}. ...
10
votes
3answers
466 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
...
6
votes
2answers
664 views
Sorting a large file
I have some huge logs, which are text files, containing up to a million rows each, and I need them sorted.
Because trying to import one obviously makes Mathematica run out of memory, we must rule out ...
12
votes
2answers
1k 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 ...
14
votes
3answers
230 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. ...
11
votes
3answers
642 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
...