Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit a5d4e57

Browse files
committedApr 11, 2013
add sort(A,dim), sortrows, and sortcols. closes #2484
1 parent d952d5e commit a5d4e57

File tree

6 files changed

+94
-10
lines changed

6 files changed

+94
-10
lines changed
 

‎base/exports.jl

+2
Original file line numberDiff line numberDiff line change
@@ -532,6 +532,8 @@ export
532532
sortby,
533533
sortby!,
534534
sortperm,
535+
sortrows,
536+
sortcols,
535537
squeeze,
536538
step,
537539
stride,

‎base/sort.jl

+39
Original file line numberDiff line numberDiff line change
@@ -408,3 +408,42 @@ sort!{O<:Direct,T<:Floats}(v::Vector{Int}, a::Algorithm, o::Perm{O,Vector{T}}) =
408408
end # module Sort.Float
409409

410410
end # module Sort
411+
412+
# sorting multi-dimensional arrays
413+
sort(A::AbstractArray, dim::Integer, o::Base.Sort.Ordering = Base.Sort.Forward(),
414+
alg::Base.Sort.Algorithm = DEFAULT_STABLE) =
415+
mapslices(sort, A, [dim])
416+
417+
sort(A::AbstractArray, dim::Integer, alg::Base.Sort.Algorithm) =
418+
sort(A, dim, Base.Sort.Forward(), alg)
419+
420+
sort(A::AbstractArray, dim::Integer, alg::Base.Sort.Algorithm, o::Base.Sort.Ordering) =
421+
sort(A, dim, o, alg)
422+
423+
function sortrows(A::AbstractMatrix, o::Base.Sort.Ordering = Base.Sort.Forward(),
424+
alg::Base.Sort.Algorithm = DEFAULT_STABLE)
425+
c = 1:size(A,2)
426+
rows = [ sub(A,i,c) for i=1:size(A,1) ]
427+
p = sortperm(rows, o, alg)
428+
A[p,:]
429+
end
430+
431+
sortrows(A::AbstractMatrix, alg::Base.Sort.Algorithm) =
432+
sortrows(A, Base.Sort.Forward(), alg)
433+
434+
sortrows(A::AbstractMatrix, alg::Base.Sort.Algorithm, o::Base.Sort.Ordering) =
435+
sortrows(A, o, alg)
436+
437+
function sortcols(A::AbstractMatrix, o::Base.Sort.Ordering = Base.Sort.Forward(),
438+
alg::Base.Sort.Algorithm = DEFAULT_STABLE)
439+
r = 1:size(A,1)
440+
cols = [ sub(A,r,i) for i=1:size(A,2) ]
441+
p = sortperm(cols, o, alg)
442+
A[:,p]
443+
end
444+
445+
sortcols(A::AbstractMatrix, alg::Base.Sort.Algorithm) =
446+
sortcols(A, Base.Sort.Forward(), alg)
447+
448+
sortcols(A::AbstractMatrix, alg::Base.Sort.Algorithm, o::Base.Sort.Ordering) =
449+
sortcols(A, o, alg)

‎doc/stdlib/base.rst

+1-1
Original file line numberDiff line numberDiff line change
@@ -485,7 +485,7 @@ Set-Like Collections
485485

486486
.. function:: IntSet(i...)
487487

488-
Construct an ``IntSet`` of the given integers. Implemented as a bit string, and therefore good for dense integer sets.
488+
Construct an ``IntSet`` of the given integers. Implemented as a bit string, and therefore designed for dense integer sets. If the set will be sparse (for example holding a single very large integer), use ``Set`` instead.
489489

490490
.. function:: union(s1,s2...)
491491

‎doc/stdlib/sort.rst

+12
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,18 @@ Sort Functions
133133
``Sort.MergeSort``, or ``Sort.TimSort``), and ``ord`` to sort with
134134
a custom ordering (e.g., Sort.Reverse or a comparison function).
135135

136+
.. function:: sort(A, dim, [alg[, ord]])
137+
138+
Sort a multidimensional array ``A`` along the given dimension.
139+
140+
.. function:: sortrows(A, [alg[, ord]])
141+
142+
Sort the rows of matrix ``A`` lexicographically.
143+
144+
.. function:: sortcols(A, [alg[, ord]])
145+
146+
Sort the columns of matrix ``A`` lexicographically.
147+
136148
-------------------------
137149
Sorting-related Functions
138150
-------------------------

‎src/sys.c

+9-9
Original file line numberDiff line numberDiff line change
@@ -206,23 +206,23 @@ DLLEXPORT double jl_stat_atime(char *statbuf)
206206

207207
DLLEXPORT double jl_stat_mtime(char *statbuf)
208208
{
209-
uv_statbuf_t *s;
210-
s = (uv_statbuf_t*) statbuf;
209+
uv_statbuf_t *s;
210+
s = (uv_statbuf_t*) statbuf;
211211
#if defined(__WIN32__)
212-
return (double)s->st_mtime;
212+
return (double)s->st_mtime;
213213
#else
214-
return (double)s->st_MTIM.tv_sec + (double)s->st_MTIM.tv_nsec * 1e-9;
214+
return (double)s->st_MTIM.tv_sec + (double)s->st_MTIM.tv_nsec * 1e-9;
215215
#endif
216216
}
217217

218218
DLLEXPORT double jl_stat_ctime(char *statbuf)
219219
{
220-
uv_statbuf_t *s;
221-
s = (uv_statbuf_t*) statbuf;
220+
uv_statbuf_t *s;
221+
s = (uv_statbuf_t*) statbuf;
222222
#if defined(__WIN32__)
223-
return (double)s->st_ctime;
223+
return (double)s->st_ctime;
224224
#else
225-
return (double)s->st_CTIM.tv_sec + (double)s->st_CTIM.tv_nsec * 1e-9;
225+
return (double)s->st_CTIM.tv_sec + (double)s->st_CTIM.tv_nsec * 1e-9;
226226
#endif
227227
}
228228

@@ -351,7 +351,7 @@ DLLEXPORT int jl_cpu_cores(void)
351351
// Returns time in nanosec
352352
DLLEXPORT uint64_t jl_hrtime(void)
353353
{
354-
return uv_hrtime();
354+
return uv_hrtime();
355355
}
356356

357357
// -- iterating the environment --

‎test/arrayops.jl

+31
Original file line numberDiff line numberDiff line change
@@ -398,3 +398,34 @@ let
398398
@test a2[i] == a[I[i]]
399399
end
400400
end
401+
402+
# sort on arrays
403+
begin
404+
local a = rand(3,3)
405+
406+
asr = sortrows(a)
407+
@test isless(asr[1,:],asr[2,:])
408+
@test isless(asr[2,:],asr[3,:])
409+
410+
asc = sortcols(a)
411+
@test isless(asc[:,1],asc[:,2])
412+
@test isless(asc[:,2],asc[:,3])
413+
414+
asr = sortrows(a, Sort.Reverse())
415+
@test isless(asr[2,:],asr[1,:])
416+
@test isless(asr[3,:],asr[2,:])
417+
418+
asc = sortcols(a, Sort.Reverse())
419+
@test isless(asc[:,2],asc[:,1])
420+
@test isless(asc[:,3],asc[:,2])
421+
422+
as = sort(a, 1)
423+
@test issorted(as[:,1])
424+
@test issorted(as[:,2])
425+
@test issorted(as[:,3])
426+
427+
as = sort(a, 2)
428+
@test issorted(as[1,:])
429+
@test issorted(as[2,:])
430+
@test issorted(as[3,:])
431+
end

0 commit comments

Comments
 (0)
Please sign in to comment.