Learn about our RFC process, Open RFC meetings & more.Join in the discussion! »

sugar-sorting

0.0.7 • Public • Published

Build Status Coverage Status

Sugar-sorting

Sorting made sweet.

NPM

What is sugar-sorting?

Sugar-sorting is a NPM Module for sorting arrays. It's main goal is to provide a simple sorting API and many different sorting methods for the user to choose from. Sugar-sorting can make it easy for you to sort simple arrays and arrays of objects.

Getting Started

The first thing you should do is install the sugar-sorting module.

$ npm install -g sugar-sorting

Now you can use require() to assign this module to a variable and start calling the module's methods.

var Sorter = require('sugar-sorting');
 
var simpleArray = [2, 4, 6, 3, 1, 5];
var sorter = new Sorter(simpleArray);
 
console.log(sorter.bubbleSort());
// --> [1, 2, 3, 4, 5, 6]

Sorting Arrays of Objects

Sugar-sorting also makes it easier for you to sort your arrays of objects by any property you want to. You just need to call the sortBy('.propertyPath') method.

var Sorter = require('sugar-sorting');
 
var students = [{
    name: 'Stanley Kubrick',
    age: 50,
    grades: {
      highestGrade: 10,
      lowestGrade: 8
    }
  }, {
    name: 'Christopher Nolan',
    age: 31,
    grades: {
      highestGrade: 8.5,
      lowestGrade: 6
    }
  }, {
      name: 'Michael Bay',
      age: 37,
      grades: {
        highestGrade: 2,
        lowestGrade: 0
      }
  }];
 
var studentsSorter = new Sorter(students);
 
var studentsByAge = studentsSorter.sortBy('.age').insertionSort();
var studentsByHighestGrade = studentsSorter.sortBy('.grades.highestGrade').bubbleSort();

Sorting methods

These are the sorting methods on sugar-sorting. The ones marked with an X are the ones available for use.

  • bubbleSort()
  • selectionSort()
  • insertionSort()
  • mergeSort()
  • heapSort()
  • quickSort()
  • shellSort()

If you want to learn more about sorting algorithms and the best and worst case scenarios for each one, you can check these amazing links:

How sugar-sorting Works

When using sugar-sorting, imagine the Sorter object as a magic box in which you put in your array and then tell the box the way you want it to be sorted. After telling the box what you want it will sort your array. Now all you need to do is get the new sorted array from inside the box.

These are some basic steps to follow:

  • Fill your sorter object using var sorter = new Sorter(yourArray) or sorter.fillWith(yourArray)
  • Tell the sorter object how you want it to be sorted. For example: sorter.sortBy('.salary').heapSort()
  • Retrieve the sorted array from the sorter object using sorter.getElements()

Contributing

You can contribute to sugar-sorting by writing tests, improving our algorithms or even creating new ones. Any improvement on the project's docs are welcome too!

To start developing just clone this repository and run the npm install command on your terminal to download all the dev-dependencies.

If you find any bug or have any suggestion please tell us using our Issue Tracker.

After changing any line of code please run tests using npm test or grunt.

License

Use it as you want to. No worries.

MIT © Lucas Fernandes da Costa

Install

npm i sugar-sorting

DownloadsWeekly Downloads

7

Version

0.0.7

License

MIT

Last publish

Collaborators

  • avatar