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 }]);Ordering Data
Section titled “Ordering Data”You can order the data in Pipeline using the .sort() method.
On Flat Values
Section titled “On Flat Values”// (1) ascending sortvar 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 sortvar 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 ]On Array of Objects
Section titled “On Array of Objects”// (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)Using Comparator Function
Section titled “Using Comparator Function”// (1) flat valuesvar 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 objectsvar 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 }// ]References
Section titled “References”Pipeline.sort()
Section titled “Pipeline.sort()”Sorts the pipeline data using an optional column name or a custom comparator function.
Signature
Section titled “Signature”class NimbusDBPipeline { // ... other methods and properties ... static sort( _ascending?: boolean ): NimbusDBPipeline;}Parameters
Section titled “Parameters”_ascending
Section titled “_ascending”- Type:
boolean - Default:
true - Optional sort direction (ascending =
true, descending =false).
Returns
Section titled “Returns”- Type:
NimbusDBPipeline - A new
NimbusDBPipelineinstance (mutable = false) or the current pipeline instance (mutable = true).
Signature
Section titled “Signature”class NimbusDBPipeline { // ... other methods and properties ... static sort( _column: string, _ascending?: boolean ): NimbusDBPipeline;}Parameters
Section titled “Parameters”_column
Section titled “_column”- Type:
string - The column name to sort by.
_ascending
Section titled “_ascending”- Type:
boolean - Default:
true - Optional sort direction (ascending =
true, descending =false).
Returns
Section titled “Returns”- Type:
NimbusDBPipeline - A new
NimbusDBPipelineinstance (mutable = false) or the current pipeline instance (mutable = true).
Signature
Section titled “Signature”class NimbusDBPipeline { // ... other methods and properties ... static sort( _func: (current: any, next: any) => number ): NimbusDBPipeline;}Parameters
Section titled “Parameters”- Type:
(current: any, next: any) => number - The comparator function to use for sorting.
Returns
Section titled “Returns”- Type:
NimbusDBPipeline - A new
NimbusDBPipelineinstance (mutable = false) or the current pipeline instance (mutable = true).