So let's say I want to group a list of points based on their distance from the origin. I would do something like:
list = {{1, 20}, {1, 6}, {1, 7}, {1, 3}, {3, 1}};
gathered = GatherBy[list, Norm[#]&]
and that would give me what I need, but that would not necessarily respect the ordering. I can apply SortBy[] first and then GatherBy[] the result:
sorted = SortBy[list, Norm[#] &]
partitionedBy = GatherBy[sorted, Norm[#] &]
But that is not very efficient. Is there any more efficient way of doing this using Mathematica's built-in functions. If not, What would be the most efficient way of achieving this?
update: if list is set to be a reduced accuracy version of an exact numerical expression, some of the methods suggested below will yield incorrect results. That is investigated in this question.



GatherBy[SortBy[list, Norm], Norm]– Bob Hanlon Feb 13 '16 at 19:48SplitBy[SortBy[list, Norm], Norm]– Bob Hanlon Feb 13 '16 at 19:54N[Norm[#] &]should beN[Norm[#]] &– Simon Woods Feb 13 '16 at 22:31GatherBy[SortBy[list2, N[Norm[#]] &], Norm]gives the correct answer unlike the typo version. – Simon Woods Feb 13 '16 at 22:51GatherByreturns lists of points, so you need to applyNormto the first element not the whole list, e.g.SortBy[GatherBy[list2, Norm], N[Norm[#[[1]]]] &]– Simon Woods Feb 13 '16 at 22:52{0., 1.64012, 0.632456, 0.632456, 1.64012, 1.58114, 0.640312, 0.6, 1.6, 0.640312, 1.58114}– Simon Woods Feb 13 '16 at 23:19NaffectsAccuracyis a quite different problem to the original question regarding efficient sort & gather algorithms. It should be a separate question. People took time and trouble to answer the original question, and it's rather unfair to claim that their (perfectly good) answers don't work because of special features in your data that you didn't reveal until several hours later. It will also be more useful to future visitors looking for efficient sort & gather algorithms if the question is unencumbered by the requirement to avoid usingN. – Simon Woods Feb 14 '16 at 11:41a==bbut False forN[a]==N[b]. – Simon Woods Feb 14 '16 at 11:42