Sort disjoint sublist: Difference between revisions
Content deleted Content added
→{{header|jq}}: "three-line solution" |
m →{{header|jq}}: simplify |
||
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):
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
| setpaths( $ix; $sorted) ;</lang>
Example:<
[7, 6, 5, 4, 3, 2, 1, 0] | disjointSort( [6, 1, 7] )</lang>produces:▼
▲[7, 6, 5, 4, 3, 2, 1, 0] | disjointSort( [6, 1, 7] )
[7,0,5,4,3,2,1,6]
|