Git Product home page Git Product logo

numbered-string-order's Introduction

numberedStringOrder

Latest Version on Packagist StyleCI Build Status

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

$ composer require awssat/numbered-string-order

Usage

Sort

//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:

 //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

$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

new numberedStringOrder();
$numberedStringOrder->englishWordsToNumbers('one hundred twenty-three thousand four hundred fifty-six');
>> output: 123456

//to get arabic words to number use: arabicWordsToNumbers(...)

Test

composer test

Currently, it supports English and Arabic.

License

The MIT License (MIT). Please see License File for more information.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.