# Sort the characters of a string in alphabetical order

Sort code points with an explicit English collation.

Canonical: https://1loc.dev/string/sort-the-characters-of-a-string-in-the-alphabetical-order/
Version: 1.0.0
Runtime: JavaScript · Node.js 22 / modern browsers
Verification: Local fixtures; no external services
Updated: 2026-10-28

## Implementation

```javascript
function sortCharacters(text) { return Array.from(text).sort((a, b) => a.localeCompare(b, 'en')).join(''); }
```

## Input

```json
"hello"
```

## Expected output

```json
"ehllo"
```

## Edge case

Input:
```json
"Bab"
```
Output:
```json
"abB"
```

## Limits

- The original used the runtime’s default locale, which can differ between machines; this version names the locale.

The lab validates types and size before calling this function. Add equivalent validation at your own trust boundary.

## Source and license

Original: https://github.com/phuocng/1loc/blob/14b481cf60dff1620ab1bb99a43b1f68068cdd9b/contents/sort-the-characters-of-a-string-in-the-alphabetical-order.mdx
Author: Nguyen Huu Phuoc and original contributors
Contributors: See original repository history
Source commit: 14b481cf60dff1620ab1bb99a43b1f68068cdd9b
Copyright (c) 2020 phuoc-ng
License: MIT — https://1loc.dev/licenses/
Maintainer: Oveyon
Modifications: Iterates code points and pins the collation locale, which the original left to the runtime default. Original MDX retained as inert source evidence.
