Sort disjoint sublist: Difference between revisions

Content deleted Content added
Peak (talk | contribs)
→‎{{header|jq}}: "three-line solution"
Peak (talk | contribs)
Line 767:
We define a jq function, disjointSort, that accepts the array of values as input,
but for clarity we first define a utility function for updating an array at multiple places:
<lang jq> def setpaths(indices; values):
def setpaths(indices; values):
if (indices|length) == 0 then .
else
Line 778 ⟶ 777:
(indices|unique) as $ix # "unique" sorts
# Set $sorted to the sorted array of values at $ix:
| ([ .[ $ix | .[] ] ] | sort) as $sorted
| setpaths( $ix; $sorted) ;</lang>
Example:</lang jq>
[7, 6, 5, 4, 3, 2, 1, 0] | disjointSort( [6, 1, 7] )</lang>produces:
Example:
<lang jq>
[7, 6, 5, 4, 3, 2, 1, 0] | disjointSort( [6, 1, 7] )
</lang>
produces:
[7,0,5,4,3,2,1,6]