Skip to content

Commit 2519c72

Browse files
committed
add view option for sorting views
1 parent 524d337 commit 2519c72

File tree

2 files changed

+15
-11
lines changed

2 files changed

+15
-11
lines changed

docs/src/man/tutorial.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -159,9 +159,9 @@ julia> delayed = @chain flights begin
159159

160160
## Reorder rows
161161

162-
Select `:IATA` and `:DepDelay` columns while sorted by `:DepDelay`. Note that in the following code, `flights[!, [:IATA, :DepDelay]]` is equivalent to `view(flights, :, [:IATA, :DepDelay])`. Further notice that calling `sort` on a view of a data set creates a new data set.
162+
Select `:IATA` and `:DepDelay` columns while sorted by `:DepDelay`. Note that in the following code, `flights[!, [:IATA, :DepDelay]]` is equivalent to `view(flights, :, [:IATA, :DepDelay])`. Further notice that, by default, calling `sort` on a view of a data set creates a new data set, however, passing `view = true` creates a view of sorted values.
163163

164-
> Note `sort!` and `sort` reorder observations instead of sorting observations by reference (i.e. view of sorted data), however, we can use `groupby` or the combination of `sortperm` and `view` to create a sorting data set by reference.
164+
> Note `sort!` and `sort` reorder observations instead of sorting observations by reference (i.e. view of sorted data), however, we can use `groupby`, the combination of `sortperm` and `view`, or pass a view of a data set and set `view = true` to create a sorting data set by reference.
165165
166166

167167
```julia

src/sort/sort.jl

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ end
8787

8888
Base.sort(ds::Dataset, col::ColumnIndex; alg = HeapSortAlg(), rev::Bool = false, mapformats::Bool = true, stable =true) = sort(ds, [col], rev = rev, alg = alg, mapformats = mapformats, stable = stable)
8989

90-
function Base.sort(ds::SubDataset, cols::MultiColumnIndex; alg = HeapSortAlg(), rev = false, mapformats::Bool = true, stable = true)
90+
function Base.sort(ds::SubDataset, cols::MultiColumnIndex; alg = HeapSortAlg(), rev = false, mapformats::Bool = true, stable = true, view = false)
9191
_check_consistency(ds)
9292
colsidx = index(ds)[cols]
9393
if rev isa AbstractVector
@@ -97,14 +97,18 @@ function Base.sort(ds::SubDataset, cols::MultiColumnIndex; alg = HeapSortAlg(),
9797
revs = repeat([rev], length(colsidx))
9898
end
9999
starts, idx, last_valid_range = _sortperm_v(ds, cols, revs, stable = stable, a = alg, mapformats = mapformats)
100-
newds = ds[idx, :]
101-
append!(index(newds).sortedcols, collect(colsidx))
102-
append!(index(newds).rev, revs)
103-
append!(index(newds).perm, idx)
104-
append!(index(newds).starts, starts)
105-
index(newds).ngroups[] = last_valid_range
106-
index(newds).fmt[] = mapformats
107-
newds
100+
if view
101+
Base.view(ds, idx, :)
102+
else
103+
newds = ds[idx, :]
104+
append!(index(newds).sortedcols, collect(colsidx))
105+
append!(index(newds).rev, revs)
106+
append!(index(newds).perm, idx)
107+
append!(index(newds).starts, starts)
108+
index(newds).ngroups[] = last_valid_range
109+
index(newds).fmt[] = mapformats
110+
newds
111+
end
108112
end
109113

110114
Base.sort(ds::SubDataset, col::ColumnIndex; alg = HeapSortAlg(), rev::Bool = false, mapformats::Bool = true, stable =true) = sort(ds, [col], rev = rev, alg = alg, mapformats = mapformats, stable = stable)

0 commit comments

Comments
 (0)