sortcoll

0.2.0 • Public • Published

sortcoll

Build Status Build Status NPM version

Simple helper to create a collection sort function for multiple keys.

NPM

Install

npm install sortcoll

Initialize

var sortcoll = require( "sortcoll" )

`sortcoll( keys, [forward=true], [fnGet] )``

Usage

var sortcoll = require( "sortcoll" )
 
var data = [  
  { id: 1, name: "Fritz" },
  { id: 2, name: "Müller" },
  { id: 3, name: "Meier" },
  { id: 4, name: "Fritz" }
];
 
var sortFunction = sortcoll( [ "name", "id" ], false );
 
var sorted = data.sort( sortFunction );
/*
[
    { id: 1, name: "Fritz" },
    { id: 4, name: "Fritz" },
    { id: 3, name: "Meier" },
    { id: 2, name: "Müller" }
]
*/

Arguments:

  • keys : ( String|String[] required ) A simple key to sort by or a list of keys sorted in the given order. Similar to sql ORDER BY name, id
  • forward : ( Boolean|Object optional: default = true ) Sort from a-z if true. Otherwise from z-a. You can set the direction for the sort keys separately by using an object. If you set a object key ? this will be used for every not undefined key.
  • fnGet : ( Function optional ) A optional function to get the sorting data from the element. Default is just el[key]

Advanced example

with fnGet and mixed forward

Here's a small example to use the fnGet argument with e.g. backbone.

var sortcoll = require( "sortcoll" )
 
var data = [  
    { id: 13, name: "Foo", age: 42 },
    { id: 1337, name: "Bar", age: 666 },
    { id: 42, name: "Fizz", age: 23 },
    { id: 23, name: "Bar", age: 23 },
    { id: 666, name: "Buzz", age: 13 }
    { id: 7, name: "Bar", age: 23 },
];
 
var fnGet = function( el, key ){
    return el.get( key )
};
 
var sortFunction = sortcoll( [ "name", "id" ], { age: false, "?": true }, fnGet );
 
myCollection = new Backbone.Collection( data, comparator: sortFunction )
 
var myCollection.toJSON();
/*
[
    { id: 1337, name: "Bar", age: 666 },
    { id: 13, name: "Foo", age: 42 },
    { id: 7, name: "Bar", age: 23 },
    { id: 23, name: "Bar", age: 23 },
    { id: 42, name: "Fizz", age: 23 },
    { id: 666, name: "Buzz", age: 13 }
]
*/

Release History

|Version|Date|Description| |:--:|:--:|:--|t |0.2.0|2016-03-22|added mixed forwarding| |0.1.0|2016-03-22|Added the fnGet option.| |0.0.1|2016-03-04|Initial commit|

NPM

Initially Generated with generator-mpnodemodule

Other projects

Name Description
node-cache Simple and fast NodeJS internal caching. Node internal in memory cache like memcached
rsmq A really simple message queue based on redis
redis-heartbeat Pulse a heartbeat to redis. This can be used to detach or attach servers to nginx or similar problems.
systemhealth Node module to run simple custom checks for your machine or it's connections. It will use redis-heartbeat to send the current state to redis.
rsmq-cli a terminal client for rsmq
rest-rsmq REST interface for.
redis-sessions An advanced session store for NodeJS and Redis
connect-redis-sessions A connect or express middleware to simply use the redis sessions. With redis sessions you can handle multiple sessions per user_id.
redis-notifications A redis based notification engine. It implements the rsmq-worker to safely create notifications and recurring reports.
nsq-logger Nsq service to read messages from all topics listed within a list of nsqlookupd services.
nsq-topics Nsq helper to poll a nsqlookupd service for all it's topics and mirror it locally.
nsq-nodes Nsq helper to poll a nsqlookupd service for all it's nodes and mirror it locally.
nsq-watch Watch one or many topics for unprocessed messages.
hyperrequest A wrapper around hyperquest to handle the results
task-queue-worker A powerful tool for background processing of tasks that are run by making standard http requests
soyer Soyer is small lib for server side use of Google Closure Templates with node.js.
grunt-soy-compile Compile Goggle Closure Templates ( SOY ) templates including the handling of XLIFF language files.
backlunr A solution to bring Backbone Collections together with the browser fulltext search engine Lunr.js
domel A simple dom helper if you want to get rid of jQuery
obj-schema Simple module to validate an object by a predefined schema

The MIT License (MIT)

Copyright © 2016 M. Peter, http://www.tcs.de

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

Readme

Keywords

none

Package Sidebar

Install

npm i sortcoll

Weekly Downloads

0

Version

0.2.0

License

MIT

Last publish

Collaborators

  • tcs-de