Tag Info

Hot answers tagged

24

This is probably because by default Sort doesn't just use numerical values, it includes structure information as well. From the doc: Numeric expressions are sorted by structure as well as numerical value: In[1]:= Sort[{Sqrt[2], 1, 2, 1/Sqrt[2]}] Out[1]= {1, 2, 1/Sqrt[2], Sqrt[2]} Sort by numerical value only: In[2]:= Sort[{Sqrt[2], 1, 2, ...


24

You can use combination of Part and Ordering as list1[[ Ordering @ list2 ]] to sort list1 in the order of list2. Examples: {list1, list2, list3} = {{1, 3, 2}, {a, b, c}, {x, y, z}}; list2[[ Ordering @ list1 ]] gives {a, c, b} and list3[[ Ordering @ list1 ]] gives {x, z, y} EDIT: Using with lists of lists, to sort the entire array based on the ...


22

n = 100; (*number of points*) s = RandomSample@Range@n; (*the initial set*) (*some aux functions*) head[{x_, xs___}] := Select[{xs}, # <= x &]; tail[{x_, xs___}] := Select[{xs}, # > x &]; (*qsort function modified for sowing the information needed*) qsort[{}] = {}; qsort[l : {x_, ___}] := Module[{lh, lt}, (Sow@{l, lh = head@l, x, lt = tail@l}; ...


18

Here is my contribution, which has the following benefits over previous answers: It sorts both numbers and non-numeric structures You can sort any column (not just the first, followed by the second, etc) You can sort in either direction (ascending / descending) Original order is kept: if you sort on the second column, the first entry will follow the order ...


16

This is short: Last /@ Sort[{Characters@#, #} & /@ names] {"~/Hex_6.dat", "~/Hex_12.dat", "~/Hex_24.dat", "~/Hex_48.dat", "~/Hex_96.dat", "~/Hex_192.dat"} Alternatively: Last /@ Sort[{ToExpression[StringJoin[Select[Characters@#, DigitQ]]], #} & /@ names]


16

Yes, there is! Szabolcs showed a use of GatherBy in an inverted fashion as a substitute for a conventional decorate-and-sort. It proved both syntactically and computationally efficient. By using that method in place of the decorate-and-sort in this application we can use Ordering directly, and also eliminate Part which was needed to strip the decoration: ...


15

I think this question admits an elegant solution. Here is my attempt: define a special wrapper: ClearAll[sortFun]; sortFun /: SortBy[expr_, sortFun[funs_List, partFun_]] := SortBy[expr, Map[Composition[#, partFun] &, funs]]; Now, mySort := {StringTake[#, 1] &, ToExpression@StringDrop[#, 1] &} and SortBy[{"T3","T14","T1","E2"}, ...


15

With: dat = {{10, b, 30}, {100, a, 40}, {1000, b, 10}, {1000, b, 70}, {100, b, 20}, {10, b, 70}}; Perhaps most directly: Cases[dat, {_, _, Max@dat[[All, 3]]}] More approaches: Last @ SplitBy[SortBy[dat, {#[[3]] &}], #[[3]] &] Pick[dat, #, Max@#] &@dat[[All, 3]] Reap[Fold[(If[#2[[3]] >= #, Sow@#2]; #2[[3]]) &, dat]][[2, 1]] Of ...


15

Rather than it being "too big" for the pattern matcher, the problem here is because of the use of //. which applies the rule repeatedly till the result no longer changes (i.e. the list is sorted). In other words, the issue is that of the complexity of the algorithm you're using (//. with ___) rather than the programming paradigm that you've chosen (pattern ...


14

No, this is not a bug, and Ordering does not give a wrong answer. Quoting the documentation of Sort, Sort[list,p] applies the function p to pairs of elements in list to determine whether they are in order. The default function p is OrderedQ[{#1, #2}] &. As you can see, Sort (and Ordering) compares using OrderedQ, not Less. OrderedQ uses a ...


14

This is implemented in SortBy: Because this function does not perform a pairwise compare, you would need to be able to recast your sort function to produce a canonical ordering. On the upside, if you are able to do so it will be far more efficient than Sort. f1 = Mod[#, 4] &; f2 = Mod[#, 7] &; SortBy[Range@10, {f1, f2}] {#, f1@#, f2@#} & ...


13

You can use ReplaceList with a helper function which has the Orderless attribute: ClearAll[f]; SetAttributes[f, Orderless]; ReplaceList[f[a, b, c], f[a___, b___, c___] :> {{a}, {b}, {c}}] // DeleteCases[#, {}, -1] & // Union // Column The DeleteCases and Union are required because the output from ReplaceList includes the empty list {} as a ...


12

Using Simon's data: In[6]:= datelist = {"29/02/2008", "15/12/2007", "06/09/2007", "06/10/2008", "05/03/2007", "24/01/2010", "19/06/2009", "03/11/2009", "02/02/2010", "25/12/2009"}; We can just sort the data by the absolute time: In[7]:= SortBy[datelist, AbsoluteTime[{#, {"Day", "Month", "Year"}}] &] Out[7]= {"05/03/2007", "06/09/2007", ...


11

Ordering and Part is more efficient than SortBy and Transpose and it can also be done in one pass as I will demonstrate. I create three lists of different type as described in the question: a = RandomInteger[999, 500]; b = RandomReal[1, 500]; c = CharacterRange["a", "z"] ~RandomChoice~ 500; I use the timeAvg function for testing: SortBy[{a, b, ...


11

A simple string-based approach is to swap the order of day/month/year, do the Sort and then swap back again: (* Example data *) datelist = DateString[# + AbsoluteTime[{2007, 01, 01}], {"Day", "/", "Month", "/", "Year"}] & /@ RandomInteger[10^8, 10] {"29/02/2008", "15/12/2007", "06/09/2007", "06/10/2008", "05/03/2007", "24/01/2010", ...


10

Something like this might be helpful. It replaces the unsorted list of symbols with a sorted list, lets Mathematica rearrange the expression in the normal way, and then applies a HoldForm before replacing the symbols back again. reorderSymbols[expr_, symbols_List] := With[{s = symbols}, HoldForm[Evaluate[expr /. Thread[s -> Sort@s]]] /. Thread[Sort@s ...


10

As you said in your comment that you just want a well displayed formula, I suggest using Row to force specific orders. A rough example will look like following, you might want to adjust the priority level according to your needs: expr = A^2 e^2 SuperMinus[\[Phi]] SuperPlus[\[Phi]] + A e SuperMinus[\[Phi]] SuperPlus[\[Phi]] Subscript[c, 2 w] Subscript[g, ...


10

How about: list /. Thread[# -> Ordering@#] &@Union@list (* {3, 1, 1, 3, 2, 2, 3, 2} *) You can use the additional arguments of Ordering to sort it the other way: list /. Thread[# -> Ordering[#, All, Greater]] &@Union@list (* {1, 3, 3, 1, 2, 2, 1, 2} *)


10

SeedRandom[42]; haystack = RandomReal[1, 6300]; AbsoluteTiming[ f = Nearest[haystack -> Range@Length@haystack]; {f[.3, 1], haystack[[f[.3, 1]]]}] (* -> {0.015625, {{3123}, {0.300033}}} *) Of course in this case most of the time is expended calculating the nearest function. If your points aren't changing from one use to the next, the time for ...


9

Note: While this whole post is just playing, the idea for the solution comes from an answer to a very real problem I had, so it wasn't just a futile exercise. The key is that (in v8) a shared function (set with SetSharedFunction) is always evaluated on the main kernel. Thanks to Andrew Moylan for pointing this out! Here's my incredibly wasteful solution ...


9

I share Leonid's reservations about basing the sort on simple string length. I would use a similar Ordering method, but I would parse things differently. Consider this test set: names = {"~/Hex_12.dat", "~/Hex_192.dat", "~/Oct_99.dat", "~/Hex_014.dat", "~/Hex_24_17.dat", "~/Hex_24_5.dat", "~/Hex_48.dat", "~/Hex_6.dat", "~/Hex_96.dat", ...


9

Position is looking for an exact match (pseudo-SameQ), rather than a numeric one. You will get the result you want with: Position[data[[All, 1]], _?(# == xlow &)] Or: Position[data[[All, 1]], x_ /; x == xlow] Generally you should use Equal (short form ==) any time you are trying to mach Real numbers, to allow for small rounding errors. Using the ...


9

Leonid Shifrin has pointed out why the 3rd sample fails: Less doesn't "compare" strings. Still, it's possible to use Sort, with the addition of OrderedQ: Sort[list, OrderedQ[{#1[[2]], #2[[2]]}] &] {{4, "a"}, {9, "b"}, {6, "k3"}, {7, "k4"}, {5, "w"}}


8

Here is my attempt to do this with a single kernel. Output is printed directly to the messages channel, and you cannot suppress the output of f /@ list because if the Dynamic expressions never display they never run. list = RandomInteger[10, 10] f = Module[{x}, (x := If[start + # < AbsoluteTime[], Print[#]; x =.]); Dynamic[Refresh[x, ...


8

Here is an implementation that uses the scheduled tasks in version 8: sleepSort[l_, t_:0.1] := Scan[RunScheduledTask[Print[#];RemoveScheduledTask[$ScheduledTask], {t #}]&, l] In a notebook environment, output will appear in the messages window. The time interval is specified as the optional second argument, defaulting to 1/10th of a second (I'm ...


8

Ok, here is my stab at it. As I mentioned in comments, I can see two ways to solve this. Ordering - based solution (and a by-product: compilable SortBy) One way is to use Ordering, but the problem here is that SortBy when used with a list of functions performs in a way where each function serves as a tie-breaker for ties resulting from application of a ...


8

Using Mathematica 7: Needs["Combinatorica`"] Partition[Flatten@ExtractCycles@FromUnorderedPairs@list, 2, 1] => {{140, 1}, {1, 2}, {2, 43}, {43, 10}, {10, 140}} Edit testLalmei = RandomSample /@ (RandomSample@ Partition[RandomInteger[{1, 1000}, 100], 2, 1, -1]); ExtractCycles@FromUnorderedPairs@testLalmei => { {643, 115, 518, 238, ...


7

You anyway need to parse your list of file names in some way. Here is how I would do it: Clear[sortByIndex]; sortByIndex[names : {__String}, pattern_] := With[{inds = StringCases[names, pattern]}, names[[Ordering[inds]]] /; FreeQ[inds, {}] ]; sortByIndex[__] := $Failed; Since I extract indices all at once with StringCases, this should be ...


7

You can avoid the error messages by specifying how your dates are defined to correctly interpret ambiguous strings such as "01/02/2007": Flatten[SortBy[ Partition[ Table[DateString[{datelist[[v]], {"Day", "Month", "Year"}}, {"Day", "/", "Month", "/", "Year"}], {v, 1, Dimensions[datelist][[1]]}], 1], AbsoluteTime[{#[[1]], {"Day", ...


7

sortF[lst_] := SortBy[lst, {Total@#, Variance[#]} &[Exponent[#, Variables[lst]]] &] lst1 = {1, x, y, x^2, x y, y^2, x^2 y, x y^2, x^2 y^2}; lst2 = {1, z, x, y, x^2, z y, z^2, x y, y^2, x^2 y, x y^2, x^2 y^2}; sortF@lst1 (* {1, x, y, x y, x^2, y^2, x^2 y, x y^2, x^2 y^2} *) sortF@lst2 (* {1, x, y, z, x y, y z, x^2, y^2, z^2, x^2 y, x y^2, x^2 ...



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