This is an automated email from the ASF dual-hosted git repository.

tustvold pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/arrow-rs.git


The following commit(s) were added to refs/heads/master by this push:
     new f875eecf0 Document how to sort a RecordBatch (#4204)
f875eecf0 is described below

commit f875eecf07ab9bc2d0425ca7170aa9178c73bd0b
Author: Raphael Taylor-Davies <[email protected]>
AuthorDate: Thu May 11 23:41:45 2023 +0100

    Document how to sort a RecordBatch (#4204)
---
 arrow-ord/src/lib.rs | 26 ++++++++++++++++++++++++++
 1 file changed, 26 insertions(+)

diff --git a/arrow-ord/src/lib.rs b/arrow-ord/src/lib.rs
index c84db09fd..62338c022 100644
--- a/arrow-ord/src/lib.rs
+++ b/arrow-ord/src/lib.rs
@@ -16,6 +16,32 @@
 // under the License.
 
 //! Arrow ordering kernels
+//!
+//! # Sort RecordBatch
+//!
+//! ```
+//! # use std::sync::Arc;
+//! # use arrow_array::*;
+//! # use arrow_array::cast::AsArray;
+//! # use arrow_array::types::Int32Type;
+//! # use arrow_ord::sort::sort_to_indices;
+//! # use arrow_select::take::take;
+//! #
+//! let a: ArrayRef = Arc::new(Int32Array::from(vec![1, 2, 3, 4]));
+//! let b: ArrayRef = Arc::new(StringArray::from(vec!["b", "a", "e", "d"]));
+//! let batch = RecordBatch::try_from_iter(vec![("a", a), ("b", b)]).unwrap();
+//!
+//! // Sort by column 1
+//! let indices = sort_to_indices(batch.column(1), None, None).unwrap();
+//!
+//! // Apply indices to batch columns
+//! let columns = batch.columns().iter().map(|c| take(&*c, &indices, 
None).unwrap()).collect();
+//! let sorted = RecordBatch::try_new(batch.schema(), columns).unwrap();
+//!
+//! let col1 = sorted.column(0).as_primitive::<Int32Type>();
+//! assert_eq!(col1.values(), &[2, 1, 4, 3]);
+//! ```
+//!
 
 pub mod comparison;
 pub mod ord;

Reply via email to