Giter Club home page Giter Club logo

thenby.js's People

Contributors

bergus avatar dependabot[bot] avatar foxhoundn avatar hagabaka avatar mh-cbon avatar shaununderwood avatar teun avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

thenby.js's Issues

very important

Usually from high to low,An empty string (" ").In general, should be at the bottom,

ThenBy for PHP

Hello. I like of how thenBy() works and I like to reimplements it on a PHP. So I want to know if I can do it based on your concept. It should be called VanillaSort, because I'm working on a big package called Vanilla: https://bitbucket.org/rentalhost/

Number Ordering

Thank you for this library, I've been using it quite a bit. Came across a sorting problem when sorting number values above 2 digits, it sorts 99 and below first only taking into account the first digit. Below is an example ordered ascending, it goes 10, 100, 101, etc for ordering.

https://jsfiddle.net/lawrencenull/xL5pc2dw/1/

Cannot use with event listener

Hi, I'm implementing thenBy.js library in my polymer application by using the event listener.
However, the sort method not update when the data changed. But the console output 'test'.
How can I implement the event listener to this library?

this.addEventListener('data-changed', (e) => this.sortOrderList(this.orderItems))
sortOrderList(order) {
                console.log('test')
                // SORT BY EDD THEN BY SPT
                let s = firstBy(function (v1, v2) { return v1.order_delivery - v2.order_delivery; })
                    .thenBy(function (v1, v2) { return v1.order_duration - v2.order_duration; });
                this.sortedOrder = order.sort(s);
                return order.sort(s);
            }` ``

Sorting does not work

Hi, maybe I am missing something but I could not get my array to sort using the example you provided. I also saw the fiddle you posted on a stackoverflow answer but the result over there is also not sorted (http://jsfiddle.net/n4ncq2nc/1/). Thanks

Github Releases?

I wish to use this in a project - hence I wanted this to be tagged.

thenBy doesn't trigger

An array of object needs to be sorted by organizer=me first, and then by order property.

It seems to me that thenBy is not getting triggered.

I've created a Fiddle from one of the similar Fiddles found here, please open the console to see that indeed "Not even run" is not showing up:
http://jsfiddle.net/u8n430rp/1/

Dynamic thenBy

Hello Teun,

thanks for this great and lightweight lib. Unfortunately I'm running into issues when not immediately chaining thenBy in the same call. E.g.

sort_stack = undefined;
sort_stack = firstBy(some_func);
sort_stack.thenBy(some_func);
some_array.sort(sort_stack);

In this case the thenBy is never executed. In my scenario I have a variable number of sort fields in an array and need to create multiple thenBy dynamically. Any idea how to do that?

BR,
toovy

Make this library ES6 + TypeScript compliant?

It would be interesting to see this library ES6 compliant and working with TypeScript.

That's a beginning of a declaration:

interface ThenBy {
    thenBy(fnc: ((v1, v2) => number) | string, direction?: number): ThenBy;
    thenBy(propertyName: string, direction?: number): ThenBy;
    (fnc: (v1, v2) => number);
}

function firstBy<T>(fnc: (v1: T, v2: T) => number, direction ?: number): ThenBy;
function firstBy<T>(propertyName: keyof T, direction ?: number): ThenBy;

Missing license

Sorry about being pedantic - I really like what you've done! Nice work!
Could you put a license on it, such as MIT [1] or other open source license, so I know if I can use it? Thanks so much!

[1] http://opensource.org/licenses/MIT

It doesn't work after firstBy

Hi I cannot make it work after the first condition.
`
this.itemsCollection.sort(

firstBy(function(a: ItemDto, b: ItemDto) {
console.log('first')
return a.title.length - b.title.length
})

.thenBy(function(a: ItemDto, b: ItemDto) {
console.log('second')
return a.text.length - b.text.length
})

.thenBy(function(a: ItemDto, b: ItemDto) {
console.log('third')
return a.itemId - b.itemId
})

)
`
It works well for the first block but not for the others. What I am doing wrong? Thanks
image

Licence

Hi Like the library any chance you could add a licence to it, such as MIT or one of the other permissive licences. Makes it easier to use your code if your working on larger open source projects where licences of are tracked and monitored.

Group Sorting

Hi,

Been using thenBy for a few weeks now and I like it alot. It simply works. I'm in a bit of a stump though trying to figure out if a specific sort is possible with this module.

I have records displayed in a table which have attributes: priority of 0 - 2 (2 being most important), person status of 0 - 4, wait time, and total time.

I'm trying to sort this information (in tiers if you will) like so:

  • tier 1: priority 2 records are always at the top sorted by wait time then total time
  • tier 2: priority != 2 and person status != 0 records sorted by wait time
  • tier 3: priority != 2 and patient status = 0 records sorted by total time

No biggie if this isn't possible. Just curious. Thanks!

TypeScript throws an error when trying to use custom compare

When using the custom compare demo code given, TypeScript throws an error.

const cards = ['2', '3', '4', '5', '6', '7', '8', '9', '10', 'J', 'Q', 'K', 'A'];
var cardCompare = (c1, c2) =>{
    return cards.indexOf(c1) - cards.indexOf(c2);
}
var handOfCards =  [
        { id: 7, suit:"c", card:"A" },
        { id: 8, suit:"d", card:"10" },
        // etc
    ];
handOfCards.sort(firstBy("card", {cmp: cardCompare, direction: "desc"}));
ERROR in src/app/opcenter/personnelfiles/personnelfiles.component.ts(103,27): error TS2345: Argument of type '{ cmp: (c1: any,
c2: any) => number; }' is not assignable to parameter of type '1 | "desc" | -1 | opt | "asc"'.
      Object literal may only specify known properties, and 'cmp' does not exist in type 'opt'.

I have literally done nothing other than copy the demo code. There seems to be an issue going on here.

Typings improvement

With typescript (v3.8.3), the compiler tells me that the second parameter of the key-param on firstBy/thenBy (i.e.: "v2") might be undefined... which just isn't true if using the (a,b)=> number form.

Imho these typings should provide a better user-experience while stille sticking to your spec:

  export function firstBy<T = any>(key: ((v1: T, v2: T) => any), direction?: number | opt): IThenBy<T>;
  export function firstBy<T = any>(key: ((v1: T) => any), direction?: number | opt): IThenBy<T>;
  export function firstBy<T = any>(key: (keyof T), direction?: number | opt): IThenBy<T>;
  // catch all implementation. Might perhaps be omitted for clarity.
  export function firstBy<T = any>(key: ((v1: T, v2?: T) => any) | keyof T, direction?: number | opt): IThenBy<T>;

Could you explain how to use it with TS in a more detailed way?

Please explain how to call it with Typescript in a more detailed way.

What i have tried:
I have added the files to my project and made the import statement.
import {firstBy} from "thenby";

How do i instantiate it/ call it now? I always get an undefined for firstBy. I have tried multiple ways but none of them worked.

An example would be great.

Thank you in advance. Great work!

global usage in Vue

I have been able to successfully use the thenBy library in a Vue file like so -
Official Scenarios Vue file:

<template>
    <div>
        {{ sortedOfficialScenarios }}
    </div>
</template>

<script>
import { firstBy } from 'thenby';

export default {
     data () {
         return {
             officialScenarios: []
         }
     },
     methods: {
         async getOfficialScenarios() {           
            this.officialScenarios = await this.$service.access('officialScenarios').get();
         }
     },
     created: function () {
         this.getOfficialScenarios();
     },
     computed: {
         sortedOfficialScenarios() {
             return (this.officialScenarios).slice().sort(
                 firstBy(function (a) { return a.year }, -1)
                 .thenBy('created', -1)
            );
         }
     }
}
</script>

thenBy is a useful library for this project so I'd like to include it globally to avoid having import statements in each Vue file. However, this is where I'm stuck. In main.js I've tried:

import { firstBy } from 'thenby';
Vue.use(firstBy);

With that code in place, and the import { firstBy} from 'thenby'; disabled in the Official Scenarios file, an error is displayed in the console that says Error in render: "ReferenceError: firstBy is not defined". I have also tried this in main.js:

import { firstBy } from 'thenby';
new Vue({
  router,
  firstBy,
  render: h => h(App)
  }
}).$mount('#app');

The error message is the same as above.

Is there a way to use thenBy globally in Vue? If so, how?

Sorting an array of Id's based on Objects (with id)

So I'm using redux, and in my store I store the rows of my table like this:

jobs: {
    byJobId: {}, //Objects indexed by id
    allJobIds: [] //Array of ids to indicate sorting order
  },

Is it possible to use this library to sort the array based on the object via its id?

Direction parameter: magic constants

What always strikes me as a bit annoying are the magic numbers for 1/-1 for up/down.
Most programming docs refer to sorting directions as "ascending" or "descending or asc/desc for short.

Might be cool to have direction/options support this as well, resulting in something like:

type SortOrder = -1 | 1 | 'asc' | 'desc';
export function firstBy(key: (v1: T, v2: T) => any), direction?: SortOrder | opt): IThenBy;

thenBy chaining, typescript infer problem

firstBy() has correctly infered type on params (a,b) but when i chain it with .thenBy() it suddenly stops infering (a, b) and goes as unknown.

Does anybody else experience that? TS: v4.4.3

I'll try attach POC in codepen later.

Provide a es6 version?

I tried to rewrite your wonderful script as a es6 module (export thenBy ... and import thenBy from ...), but I fail shamefully. So I wonder if there is a chance to motivate you to provide a script that follows that convention?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.