# Questions tagged [sorting]

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

259 questions
Filter by
Sorted by
Tagged with
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 ...
1answer
575 views

### Windows Explorer Natural Order

Currently there are a list of notebooks in Windows that I am processing in a folder. Ideally I would like the notebooks to be concated in the same order as how they display in Windows Explorer. I ...
2answers
509 views

### How to sort Table/Matrix including legend(Labellings)

I want to sort table including its legend for graph I have table similar to M1 M2 M3 1 5 2 3 1 4 4 2 -10 5 7 11 6 -2 1 ...
1answer
197 views

### Problem with Sort - unexpected order with Sqrt [duplicate]

Sort[{13, Sqrt, Sqrt}] (*{13, Sqrt, Sqrt}*) Sort[{13, Sqrt, Sqrt}, #1 < #2 &] (*{Sqrt, Sqrt, 13}*) This seems ...
1answer
1k views

### How to implement Kemeny-Young method? (rank aggregation problem)

Prehistory I am trying to make some statistical analysis of some experimental data, arises from measurements made ​​on an ordinal scale. I faced with the problem of rank aggregation: to get from ...
1answer
369 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)/...
1answer
277 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 ...
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 ...
1answer
317 views

### Another Sort with multiple criterion

I have a list ...
3answers
1k views

### Properly ordering the output of FileNames for a directory consisting of files with consecutive integer names [duplicate]

Say I have a number of files in a directory at some location (C:\dir1\dir2\dir3...), and I pull an array of strings corresponding to the file names with the command: ...
6answers
452 views

### Sort a list of coordinates by frequency from first component of each coordinate

The situation is that I have a List like this: ...
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 ...
2answers
485 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 ...
2answers
268 views

### Are there functions or packages in Mathematica to sort subsets in lex order, colex, revlex, etc.?

I am looking for non-canonical sorts of lists. A function that would give: Sort[X, "order type"] With "order type" being colex, revlex, etc.
1answer
148 views

### Ordering under assumptions

I have a three-element list which contains inverses of a function u, like so: ...
2answers
225 views

### Why does Sort fail in this simple example?

I'm sure I'll be embarrassed by the answer, but I don't understand why I get the following result: list = {{9, "b"}, {4, "a"}, {7, "k4"}, {6, "k3"}, {5, "w"}} ...
4answers
860 views

### Gather list elements by labels

The following list has some elements that are labeled. For example {1, 2} -> 1, {-1, 3} -> 3, etc: ...
1answer
377 views

### Sorting a list of triplets by first element of each triplet fails

In the last line of code, I´m trying to order the matrix by the first column. I want to store in the Array t2tlistsorted ( 100 x 3 ), but I can´t. ...
2answers
206 views

### Non-canonical re-ordering of nested lists

I have some data in the form: ...
6answers
884 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 ...
5answers
485 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: ...
1answer
305 views

### Using Sort vs SortBy

So I have two lists, A and B. I put them into lists = {A,B}I want to sort A such that Im[#1]<Im[#2]& which I can do fine ...
1answer
205 views

### How to tell when a job is too big for the pattern matcher?

Pattern matching is great but it has its limitations. Consider sorting a list of numbers from smallest to largest: ...
1answer
2k views

### How do I sort complex numbers?

Okay, so I posted something about sorting, and it was linked as a duplicate to This post However, I am having problems trying to sort a list that contains both real and complex numbers. I put them ...
1answer
182 views

### Sorting the element in a PDB text file

I am a new user of Mathematica and currently using version 9. I want to rearrange the element of one PDB text file with respect to another PDB text file. Suppose I have a PDB file like this: ...
0answers
313 views

### Negative power instead of fraction [duplicate]

Solve returns a solution in the form {{ x -> y / a^2 + y^2 / a^7 }}. Since I want to process the input (with another program) in terms of Laurent polynomials, I ...
3answers
926 views

### Order list of monomials by degree (Pascal's Triangle)

Suppose I have the following list of monomials: {1,x,x^2,y,y^2,x*y,x^2*y,x*y^2,x^2*y^2} How could I manipulate it to get ...
2answers
341 views

### Sorting a matrix alphanumerically

Somewhat related to Sort matrix by columns and rows without changing them, but more general. I'd like to sort a square matrix (a 3 by 3 in my case, but surely the general solution will treat any), ...
1answer
356 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: ...
3answers
465 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 ...
1answer
791 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: ...
1answer
127 views

### Confused behavior using Dispatch to sort one list relative to another

I have a need to sort the entries of one list into the same sequence as the entries in a reference list. Because I have rather large lists involving strings, I'm trying to use Dispatch to accomplish ...
5answers
3k 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: ...
1answer
772 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$ ...
3answers
2k views

### Non Commutative sorting in Mathematica

I have been trying to do some oscillator algebra in Mathematica and having troubles with sorting and simplifying expressions. I am using the NCAlgebra package which helps to improve the properties of <...
2answers
775 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 ...
2answers
2k views

### Sort matrix by columns and rows without changing them

I would like to sort a matrix in descending order first by the total of each column, then by the total of each row, but without changing their content. For example, if I had: ...
6answers
508 views

### Sorting Matrix elements

I have matrix in as shown, consisting of real numbers and 0. How can I sort it to become out as shown? ...
1answer
832 views

### How to Sort on row by row basis?

Say I have matrix how to sort on row by row basis to get:
1answer
335 views

### Why can't I remove Orderless from Plus?

I am making some random algebra equations, and I want to have the ordering be random too, such that if my random equation generator makes x+1, the output stays <...
2answers
237 views

### Sorting list with variable

I want to sort (or sortby) the list. List contains uninitialized variable m and I can assume that m is a Natural number that is much larger then any constant. ...
1answer
529 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, ...
4answers
481 views

### How to pick out the element in a list of lists which satisfies a particular condition?

I have got a list: ...
2answers
401 views

### Merging different size, different order table

I'm a beginner in Mathematica, so this may be a simple request. I have generated some columns of names and data. I want to merge them into a single list of names, with a column for each attribute. ...
4answers
2k 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 ...
2answers
489 views

### Mathematica list element sort

I have a list, say, ...
11answers
2k 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 ...
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, ...
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 ...
4answers
9k 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 ...