Hot answers tagged

9

You say I've defined ordering function...which always return True or False. But I don't think that's enough for it to be an ordering function. Consider this example, which also doesn't give the results you expect, myOrderingFunction[arg1_, arg2_] := EvenQ[Round[Sin[ arg1 arg2]]]; list = RandomReal[20, 100]; OrderedQ[Sort[list, myOrderingFunction], ...


8

There were comments about the lack of an OrderingBy function, and for this I'll quote Szabolcs I think that OrderingBy is not necessary. It it were to be analogous to SortBy then OrderingBy[list, f] would give exactly the same output as Ordering[f /@ list], which can even be changed to So we can just write orderingBy[list_, f_] := Ordering[f /@ ...


4

This basically directly computes the angle that a line drawn from the origin to the point makes with the horizontal axis, and uses this to sort: list = {{100, 200}, {200, 300}, {300, 300}, {320, 150}, {250, 210}, {350, 220}, {380, 100}, {390, 300}}; ordList = SortBy[list, Apply[N[ArcTan[#1, #2]] &]] (* {{380, 100}, {320, 150}, {350, 220}, {390, 300}, ...


4

As noted by @SimonWoods in the comments, using #.#& instead of Norm gives a huge speed up. ClearAll[f1, f1b, f2, f2b, f3, f3b, f4, f5, f6, f7, f8] f1 = GatherBy[SortBy[#, N@Norm@# &], N@Norm@# &] &; f2 = SplitBy[SortBy[#, N@Norm@# &], N@Norm@# &] &; f3 = SortBy[GatherBy[#, N[Norm@#] &], N[Norm[#[[1]]]] &] &; f1b = ...


4

This is more than 50% faster, since it evaluates only one Norm for each sublist: AbsoluteTiming[list3 = SortBy[GatherBy[list, Norm], N[Norm[#[[1]]]] &];]


4

list = RandomInteger[{0, 20}, {10000, 2}]; AbsoluteTiming[list2 = GatherBy[SortBy[list, N[Norm[#]] &], Norm];] (* {0.149292, Null} *) SplitBy will partition without additional sorting; however, it is nonetheless slower. AbsoluteTiming[list3 = SplitBy[SortBy[list, N[Norm[#]] &], Norm];] (* {0.212032, Null} *) Verifiying that the two ...


1

a = {"10", "1007680", "1007690", "1007700", "1007710", "1007720", "1007730", "1007740", "1032540", "1032550", "1032560", "1032570", "1032580", "1032590", "1032600", "1032610", "11250", "11260", "11280", "11300", "134110", "134120", "134130", "134140", "134150", "20", "30", "301720", "301730", "301740", "301750", "301760", "38560", "38570", ...



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