lazyarray

LazyArrays allow you to sort their elements by any of its properties values in ascending/descending order, you can use deep properties.


Keywords
lazyarray, lazy, array, sorting, sort
License
Apache-2.0
Install
npm install lazyarray@1.0.2

Documentation

Installation

$ npm install lazyarray

Usage:

  var LazyArray = require('lazyarray');

  var myElements = [
      { age: 50, name: "Jack Bauer" }, 
      { age: 40, name: "Hank Moody" }, 
      { age: 30, name: "Mickey Knox" },
  ];

  var myLazyArray = new LazyArray(myElements);
  console.log(myLazyArray); 

  // console.log output: LazyArray { 
  //   '0': { age: 50, name: 'Jack Bauer' }, 
  //   '1': { age: 40, name: 'Hank Moody' },
  //   '2': { age: 30, name: 'Mickey Knox' },
  //   length: 3 }

  myLazyArray.sort("age", "ascending");
  console.log(myLazyArray);

  // console.log output: LazyArray { 
  //   '0': { age: 30, name: 'Mickey Knox' },
  //   '1': { age: 40, name: 'Hank Moody' },
  //   '2': { age: 50, name: 'Jack Bauer' },
  //   length: 3 }

  myLazyArray.sort("name", "descending");
  console.log(myLazyArray);

  // console.log output: LazyArray { 
  //   '0': { age: 30, name: 'Mickey Knox' },
  //   '1': { age: 50, name: 'Jack Bauer' },
  //   '2': { age: 40, name: 'Hank Moody' },
  //   length: 3 }

  var myElements = [
      { id: [{ id: [0, 10] }, 10, 20], element: 0 }, 
      { id: [{ id: [1, 10] }, 11, 20], element: 1 }
  ];

  var myLazyArray = new LazyArray(myElements);
  console.log(myLazyArray);

  // LazyArray {
  //  '0': { id: [ [Object], 10, 20 ], element: 0 },
  //  '1': { id: [ [Object], 11, 20 ], element: 1 },
  //  length: 2 }

  myLazyArray.sort('id[0].id[0]', 'descending');
  console.log(myLazyArray);

  // LazyArray {
  //   '1': { id: [ [Object], 11, 20 ], element: 1 },
  //   '0': { id: [ [Object], 10, 20 ], element: 0 },
  //   length: 2 }

  myLazyArray.sort('id[1]', 'ascending');
  console.log(myLazyArray);

  // LazyArray {
  //   '1': { id: [ [Object], 10, 20 ], element: 0 },
  //   '0': { id: [ [Object], 11, 20 ], element: 1 },
  //   length: 2 }

  myLazyArray.sort('element', 'descending');
  console.log(myLazyArray);

  // LazyArray {
  //   '1': { id: [ [Object], 11, 20 ], element: 1 },
  //   '0': { id: [ [Object], 10, 20 ], element: 0 },
  //   length: 2 }

Authors

See also the list of contributors who participated in this project.

License

This project is licensed under the Apache License