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 🙏

© 2025 – Pkg Stats / Ryan Hefner

compare-two-string

v1.0.3

Published

compare two string

Readme

字符串排序

对数字, 字母, 汉字等混合数据, 进行混排

功能需求

字符串可能的类型

  1. 数字字符串
  2. 英文字符串
  3. 中文字符串
  4. 数字中文英文混排

排序条件

  1. [数字] 按照数值大小排序
  2. [字母] 按照字母顺序排序
  3. [中文] 可以按照 ascii 或者 字符首字母字符顺序进行排列
  4. 数字在英文字符前面
  5. 字母在中文字符前面
  6. 一个字符串包含, 数字, 字母, 中文, 则遍历该字符串依次按照前几条规则进行排序

其他:

  1. 字符串中的数字, 单个数值依此比较: 如 a1, a12, a2, a13 --> a1, a12, a13 a2
  2. 字符串中的数字, 取出连续数字进行比较: 如 a1, a12, a2, a13 --> a1, a2, a12
  3. 中文字符同音字的处理

安装

npm install compare-two-string

使用

import compareTwoString from 'compareTwoString';

const arr = ['add12', 'add2', 'add1', 'cae', 'nihao'];
const options = {
  sortByPinyin: false,
  sortByNumericalSize: false,
};
// up sort
arr.sort((a, b) => compareTwoString(a, b, options)); // ['add1', 'add12', 'add2', 'cae', 'nihao']

// down sort
arr.sort((a, b) => -1 * compareTwoString(a, b, options)); // ['nihao', 'cae', 'add2', 'add12', 'add1']

参数说明

| 参数名 | 默认值 | 释意 | | ------------------- | ------ | ------------------------------------------------------------------------------------------------------------------------- | | sortByPinyin | false | 默认按照中文字符串的 ASCII 值进行排序, 如果设置为 true, 则按照中文字符串 首字母列表顺序进行排序 | | sortByNumericalSize | false | 包含有数字的字符串(除去纯数字的字符串), 默认按照单个数字大小进行排序, 如果设置为 true, 则按照连续数字字符串的大小进行排序 |