Skip to content

Data Ordering in Pipelines

In this section, we’ll explore how to order your data in Pipeline. We’ll use items model for this example.

var schema = {
id: {
type: NIMBUSDB_DATA_TYPE.INTEGER,
const: NIMBUSDB_CONSTRAINT.PRIMARY_KEY
},
name: NIMBUSDB_DATA_TYPE.STRING,
price: {
type: NIMBUSDB_DATA_TYPE.NUMBER,
validator: function(data, value) {
return value >= 0;
},
default_value: 0
},
is_locked: {
type: NIMBUSDB_DATA_TYPE.BOOLEAN,
const: NIMBUSDB_CONSTRAINT.OPTIONAL,
default_value: false
}
};
items = new NimbusDBModel("global", "items", schema, [
{ id: 1, name: "Apple", price: 5 },
{ id: 2, name: "Banana", price: 7.2 },
{ id: 3, name: "Cherry", price: 15 },
{ id: 4, name: "Date", price: 12.5 },
{ id: 5, name: "Elderberry", price: 8 },
{ id: 6, name: "Fig", price: 10 },
{ id: 7, name: "Grape", price: 6 },
{ id: 8, name: "Honeydew", price: 9 },
{ id: 9, name: "Kiwi", price: 4 },
{ id: 10, name: "Lemon", price: 3 }
]);

You can order the data in Pipeline using the .sort() method.

// (1) ascending sort
var items_pl = items.pipe() // create an `items` pipeline
// let's say you have this data in your pipeline:
// [ 5, 7.2, 15, 12.5, 8, 10, 6, 9, 4, 3 ]
.sort(); // sort the data (ascending)
// result:
// [ 3, 4, 5, 6, 7.2, 8, 9, 10, 12.5, 15 ]
// (2) descending sort
var items_pl = items.pipe() // create an `items` pipeline
// let's say you have this data in your pipeline:
// [ 5, 7.2, 15, 12.5, 8, 10, 6, 9, 4, 3 ]
.sort(false); // sort the data (descending)
// result:
// [ 15, 12.5, 10, 9, 8, 7.2, 6, 5, 4, 3 ]
// (1) sort by a column (ascending)
var items_pl = items.pipe() // create an `items` pipeline
// let's say you have this data in your pipeline:
// [
// { id: 1, name: "Apple", price: 5 },
// { id: 2, name: "Banana", price: 7.2 },
// { id: 3, name: "Cherry", price: 15 },
// { id: 4, name: "Date", price: 12.5 }
// ]
.sort("price"); // sort the data by the `price` column (ascending)
// result (inherited data):
// [
// { id: 1, name: "Apple", price: 5 },
// { id: 2, name: "Banana", price: 7.2 },
// { id: 4, name: "Date", price: 12.5 },
// { id: 3, name: "Cherry", price: 15 }
// ]
// (2) sort by a column (descending)
var items_pl = items.pipe() // create an `items` pipeline
// let's say you have this data in your pipeline:
// [
// { id: 3, name: "Cherry", price: 15 },
// { id: 4, name: "Date", price: 12.5 },
// { id: 2, name: "Banana", price: 7.2 },
// { id: 1, name: "Apple", price: 5 }
// ]
.sort("price", false); // sort the data by the `price` column (descending)
// (1) flat values
var items_pl = items.pipe() // create an `items` pipeline
// let's say you have this data in your pipeline:
// [ 5, 7.2, 15, 12.5, 8, 10, 6, 9, 4, 3 ]
.sort(function(a, b) {
return a - b; // return the difference between `a` and `b`
});
// result:
// [ 3, 4, 5, 6, 7.2, 8, 9, 10, 12.5, 15 ]
// (2) array of objects
var items_pl = items.pipe() // create an `items` pipeline
// let's say you have this data in your pipeline:
// [
// { id: 1, name: "Apple", price: 5 },
// { id: 2, name: "Banana", price: 7.2 },
// { id: 3, name: "Cherry", price: 15 },
// { id: 4, name: "Date", price: 12.5 }
// ]
.sort(function(a, b) {
return b.price - a.price; // return the difference between `b.price` and `a.price`
});
// result (inherited data):
// [
// { id: 3, name: "Cherry", price: 15 },
// { id: 4, name: "Date", price: 12.5 },
// { id: 2, name: "Banana", price: 7.2 },
// { id: 1, name: "Apple", price: 5 }
// ]

Sorts the pipeline data using an optional column name or a custom comparator function.

pipeline.d.ts
class NimbusDBPipeline {
// ... other methods and properties ...
static sort(
_ascending?: boolean
): NimbusDBPipeline;
}
  • Type: boolean
  • Default: true
  • Optional sort direction (ascending = true, descending = false).
  • Type: NimbusDBPipeline
  • A new NimbusDBPipeline instance (mutable = false) or the current pipeline instance (mutable = true).