npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

xl-sort

v2.0.1

Published

Multi-column sorting

Downloads

27

Readme

应用场景- 树形结构数据 多列排序

const TreeData = [
  {
    base: "base1",
    BQ: 1,
    V1: 12,
    V2: 3,
    CUS: "LH002",
    Date: "2022-01-25 18:17:38.861",
    children: [
      {
        base: "base1",
        BQ: 1,
        V1: 12,
        V2: 3,
        Date: "2050-01-25 18:17:38.861",
        children: [
          {
            base: "base1",
            BQ: 1,
            V1: 12,
            V2: 3,
            CUS: "LH002",
            Date: "2022-2-25 18:17:38.861",
            children: [],
          },
          {
            base: "base1",
            BQ: 1,
            V1: 22,
            V2: 3,
            CUS: "LH002",
            Date: "2022-03-25 18:17:38.861",
            children: [],
          },
        ],
      },
      {
        base: "base1",
        BQ: 1,
        V1: 22,
        V2: 3,
        Date: "2022-07-25 18:17:38.861",
        children: [],
      },
      {
        base: "base2",
        BQ: 1,
        V1: 123,
        V2: 3,
        Date: "2022-07-01 18:17:38.861",
        children: [],
      },
    ],
  },
  {
    base: "base1",
    BQ: 2,
    V1: 22,
    V2: 3,
    CUS: "LH003",
    Date: "2022-11-25 18:17:38.861",
    children: [
      {
        base: "base1",
        BQ: 1,
        V1: 12,
        V2: 3,
        CUS: "LH004",
        Date: "2022-10-15 18:17:38.861",
        children: [],
      },
      {
        base: "base2",
        BQ: 1,
        V1: 22,
        V2: 3,
        CUS: "LH005",
        Date: "2022-10-22 18:17:38.861",
        children: [],
      },
    ],
  },
  {
    base: "base2",
    BQ: 1,
    V1: 123,
    V2: 3,
    CUS: "LH012",
    Date: "2022-10-25 18:17:38.861",
    children: [],
  },
  {
    base: "base2",
    BQ: 2,
    V1: 867,
    V2: 3,
    CUS: "LH013",
    Date: "2025-10-25 18:17:38.861",
    children: [],
  },
];

导入

import XLSort from "XLSort";
const sortInstance = new XLSort();

sub 和 dispatch 时 type 必须一致 如: 'CUTSOM' 排序默认是递归节点下有 children

  // 排序规则
    interface SortConfig {
      configId: Number,
      order: 'desc' | 'asc',
      field: String | (Node:Object):String,
      sortType: 'string' | 'number' | 'date'
    }

简单多列排序

// 添加排序规则
sortInstance.subConf("CUSTOM", {
  configId: 0, // 主键 用于更新删除排序规则
  order: "asc", // "asc"升序 或者 "desc"降序
  field: "base", // 指定以base字段排序
  sortType: "string", // 指定字段排序类型 三种 'string' 或 'number' 或者 'date'
});
sortInstance.subConf("CUSTOM", {
  configId: 1,
  order: "desc",
  field: "Date",
  sortType: "date",
});
// 排序(不改变原数组)
sortInstance.dispatchSort("CUSTOM", {
  data: TreeData,
});

自定义条件多列排序

// 添加排序规则
sortInstance.subConf("CUSTOM2", {
  configId: 0, // 主键用于 更新 删除排序规则
  order: "asc", // "asc"升序 或者 "desc"降序
  field: "base", // 指定以base字段排序 类型为 string || function
  sortType: "string", // 指定字段排序类型 三种 'string' 或 'number' 或者 'date'
});
sortInstance.subConf("CUSTOM2", {
  configId: 1,
  order: "desc",
  sortType: "date",
  // 自定义字段排序
  field: (Node) => {
    if (Node.BQ === 1) {
      // 当前节点BQ的值为1 则取节点 V1 的值
      return "V1";
    }
    if (Node.BQ === 2) {
      // 当前节点BQ的值为2 则取节点 V2 的值
      return "V2";
    }
  },
});
sortInstance.dispatchSort("CUSTOM2", {
  data: TreeData,
});

删除规则

// 删除 sub添加的 configId为 2 的规则
sortInstance.deleteConf("CUSTOM", 2);

更新规则

// 更新 sub添加的 configId为 0 的规则
sortInstance.updateConf("CUSTOM", 0, {
  configId: 2,
  order: "desc", // 更新order为'desc'
  // ...
});

排序

sortInstance.dispatchSort("CUSTOM", {
  data: TreeData,
  childFieldAlias: "child", // 可选 递归时子节点别名设置 children => child
});

其他

// 添加基底排序
// CUSTOM上的排序规则 都会以在基底排序之上 再排序
sortInstance.subBaseConf("CUSTOM", {
  configId: 0,
  order: "desc",
  field: "base",
  sortType: "string",
});
// 添加默认排序 如果CUSTOM规则中 已经subConf新加的自定义排序 那么默认排序将会失效 所以最终的排序规则为 最终排序的规则 BaseConf + DefaultConf || Conf
sortInstance.subDefaultConf("CUSTOM", {
  configId: 2,
  order: "desc",
  sortType: "number",
  field: (Node) => {
    if (Node.BQ === 1) {
      return "V2";
    }
    if (Node.BQ === 2) {
      return "V1";
    }
  },
});