https://github.com/awssat/numbered-string-order
๐ข Sort an array of strings based on the included numbers naturally.
https://github.com/awssat/numbered-string-order
arabic array episodes natsort natural-sort order orderby php sort sorting-algorithms string
Last synced: about 2 months ago
JSON representation
๐ข Sort an array of strings based on the included numbers naturally.
- Host: GitHub
- URL: https://github.com/awssat/numbered-string-order
- Owner: awssat
- License: mit
- Created: 2017-11-15T01:02:24.000Z (over 7 years ago)
- Default Branch: master
- Last Pushed: 2019-07-15T11:45:58.000Z (almost 6 years ago)
- Last Synced: 2025-03-24T12:47:47.998Z (2 months ago)
- Topics: arabic, array, episodes, natsort, natural-sort, order, orderby, php, sort, sorting-algorithms, string
- Language: PHP
- Homepage:
- Size: 25.4 KB
- Stars: 7
- Watchers: 2
- Forks: 2
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- License: LICENSE.md
Awesome Lists containing this project
README
# numberedStringOrder
[](https://packagist.org/packages/awssat/numbered-string-order)
[](https://styleci.io/repos/110764857)
[](https://travis-ci.org/awssat/numberedStringOrder)Sort an array of strings based on the included numbers naturally. An alternative to PHP built-in natsort function that's actually work.
## Install
Via Composer
``` bash
$ composer require awssat/numbered-string-order
```## Usage
#### Sort
```php
//if you are using laravel, then instead use:
//$numberedStringOrder = app(Awssat\numberedStringOrder\numberedStringOrder::class);$numberedStringOrder = new numberedStringOrder();
$numberedStringOrder->sort([
'episode 5',
'episode50',
'499',
'episode1',
'episode two hundred',
'episode one',
'episode two',
'episode eleven',
'episode three'
]);
>> output:
[
"episode1",
"episode one",
"episode two",
"episode three",
"episode 5",
"episode eleven",
"episode50",
"episode two hundred",
499,
]
```If you ask why not use the built-in function (natsort), then see the natsort output of the same example above to know why:
```php
//output of built-in function natsor(): ๐คจ
[
"499",
"episode1",
"episode 5",
"episode50",
"episode eleven",
"episode one",
"episode three",
"episode two",
"episode two hundred",
]
```
#### Get the numbers
If you want to use our internal numbers extracting method```php
$numberedStringOrder->getNumbers(['2digits', 'text1', 'three3', 'two cars', 'blank']);>> output:
[
"2digits" => 2,
"text1" => "1",
"three3" => "3",
"two cars" => 2,
"blank" => "blank",
]
```#### Convert words to numbers
This package can also be helpful if you want to convert numerical words to numbers```php
new numberedStringOrder();
$numberedStringOrder->englishWordsToNumbers('one hundred twenty-three thousand four hundred fifty-six');
>> output: 123456//to get arabic words to number use: arabicWordsToNumbers(...)
```
## Test
```bash
composer test
```Currently, it supports English and Arabic.
## License
The MIT License (MIT). Please see [License File](LICENSE.md) for more information.