Socket
Socket
Sign inDemoInstall

alpha-sortable

Package Overview
Dependencies
3
Maintainers
1
Versions
1
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

    alpha-sortable

Encode/decode numbers in strings so that they sort properly


Version published
Weekly downloads
2
increased by100%
Maintainers
1
Install size
364 kB
Created
Weekly downloads
 

Readme

Source

alpha-sortable

NPM version

Encode/decode numbers in strings so that they sort properly

Why?

  • Because sometimes you want items sorted in numeric order, even though they are sorted on a text field. Example: filenames.
  • Zero padding only works if your users are okay with lots of zeros, and/or you know your numbers will never get large.

How?

  1. Zero-pad to a reasonable amount -- like how big you think things will probably get in the life of the system
  2. When you get bigger numbers, prefix them with a flagged length that keeps them sorting properly. The flag is an 'E' for every digit in the length counter, followed by the length counter, followed by a hyphen.

A few examples (more below):

NumberEncoded with default setting {length: 4}
10001
100010
1000100
10001000
10000E5-10000
100000000E9-100000000
1000000000EE10-1000000000
1e+100EEE101-10000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000

API

import {stringify, parse} from 'alpha-sortable'

console.log(stringify(20))
// => 0020

console.log(stringify(20, {length: 2}))
// => 20

console.log(stringify(20000000))
// => E8-20000000

console.log(parse('E8-20000000') === 20000000)
// true

More Examples

Here's the full output of demo-table.js :

NumberBigIntencoded {length: 4}
00n0000
11n0001
99n0009
1010n0010
9999n0099
100100n0100
999999n0999
10001000n1000
99999999n9999
1000010000nE5-10000
9999999999nE5-99999
100000100000nE6-100000
999999999999nE6-999999
10000001000000nE7-1000000
99999999999999nE7-9999999
1000000010000000nE8-10000000
9999999999999999nE8-99999999
100000000100000000nE9-100000000
999999999999999999nE9-999999999
10000000001000000000nEE10-1000000000
99999999999999999999nEE10-9999999999
1000000000010000000000nEE11-10000000000
9999999999999999999999nEE11-99999999999
100000000000100000000000nEE12-100000000000
999999999999999999999999nEE12-999999999999
10000000000001000000000000nEE13-1000000000000
99999999999999999999999999nEE13-9999999999999
1000000000000010000000000000nEE14-10000000000000
9999999999999999999999999999nEE14-99999999999999
100000000000000100000000000000nEE15-100000000000000
999999999999999999999999999999nEE15-999999999999999
10000000000000001000000000000000nEE16-1000000000000000
100000000000000009999999999999999nEE16-9999999999999999
1000000000000000010000000000000000nEE17-10000000000000000
10000000000000000099999999999999999nEE17-99999999999999999
100000000000000000100000000000000000nEE18-100000000000000000
1000000000000000000999999999999999999nEE18-999999999999999999
10000000000000000001000000000000000000nEE19-1000000000000000000
100000000000000000009999999999999999999nEE19-9999999999999999999
1000000000000000000010000000000000000000nEE20-10000000000000000000
10000000000000000000099999999999999999999nEE20-99999999999999999999
100000000000000000000100000000000000000000nEE21-100000000000000000000
1e+21999999999999999999999nEE21-999999999999999999999
1e+211000000000000000000000nEE22-1000000000000000000000
1e+229999999999999999999999nEE22-9999999999999999999999
1e+2210000000000000000000000nEE23-10000000000000000000000
1e+2399999999999999999999999nEE23-99999999999999999999999
1e+23100000000000000000000000nEE24-100000000000000000000000
1e+24999999999999999999999999nEE24-999999999999999999999999
1e+241000000000000000000000000nEE25-1000000000000000000000000
1e+259999999999999999999999999nEE25-9999999999999999999999999
1e+2510000000000000000000000000nEE26-10000000000000000000000000

FAQ

Q. Isn't there some standard way to do this?

A. Not that I could find.

Q. Is there a maximum number it can handle?

A. No. For example, it could handle a googolplex by starting with a googol 'E' characters. No problem! (Of course, no computer in this universe will ever be able to hold a number as large as a googolplex.)

Q. Why 'E'

A. It has to be a character that sorts after 9, which is most of them. Since the notation is very similar to scientific notation, 'E' seemed like a good idea. We also want it to be a character that's going to be legal in pretty much every context, including URLs. 'E' and hyphen seem to pass that test.

Q. Does the 'E' have to be uppercase?

A. Let's say yes. We don't have a clear example of where a lowercase E would break something, but it seems like a bad idea.

Keywords

FAQs

Last updated on 26 Oct 2021

Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc