Laravel/PHP:按字母顺序和数字排序

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/26493608/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 10:18:48  来源:igfitidea点击:

Laravel/ PHP: Order By Alphabetical with numbers in order

phpmysqllaravel

提问by Toby Mellor

When ordering things by Alphabetical Order, I'm left with this:

按字母顺序排序时,我只剩下这个:

S1 Episode 1
S1 Episode 11
S1 Episode 12
S1 Episode 2
S1 Episode 3

S2 Episode 1
S2 Episode 11

Example Code:

示例代码:

DB::table('test')->orderby('title', 'ASC')->get();

Etc. I need these to be ordered properly. Any solutions?

等等。我需要正确订购这些。任何解决方案?

Thanks.

谢谢。

回答by sjagr

You are being posed with the problem of sorting items alphanumerically, or in computer science terms, natural sorting.

您正面临按字母数字排序项目的问题,或者用计算机科学术语来说,自然排序。

There are many ways to achieve a natural sort with straight MySQLbut you could also take the results from your Laravel helper into array format and implement PHP's natsortfunction instead.

很多方法可以使用直接的 MySQL 实现自然排序,但您也可以将 Laravel 助手的结果转换为数组格式并实现 PHP 的natsort功能

From the methods I found above, I derived the best way that would likely solve your problem with the example code:

从我在上面找到的方法中,我得出了可能会使用示例代码解决您的问题的最佳方法:

DB::table('test')->orderBy('LENGTH(title)', 'ASC')
    ->orderBy('title', 'ASC')
    ->get();

however I'm not sure if the helper will complain about receiving a MySQL function instead of a straight column name into the orderByfunction. I'm only transcribing from the references I used in combination with your example too - I cannot guarantee the efficacy.

但是我不确定帮助程序是否会抱怨在函数中接收到 MySQL 函数而不是直接的列名orderBy。我也只是从我与您的示例结合使用的参考文献中转录 - 我不能保证有效性。

回答by Erhnam

For Laravel this also works:

对于 Laravel,这也适用:

$collection = $collection->sortBy('order', SORT_REGULAR, true);

回答by Joesel Duazo

It might be late but for others it might help.

可能会迟到,但对其他人来说可能会有所帮助。

Based on above link I found below, I derived the best way that would likely solve your problem with the example code: https://www.electrictoolbox.com/mysql-order-string-as-int/

根据我在下面找到的上述链接,我得出了可能使用示例代码解决您的问题的最佳方法:https: //www.electrictoolbox.com/mysql-order-string-as-int/

Query

询问

SELECT * FROM <table> ORDER BY CAST(<column> AS unsigned)

Example for laravel

laravel 的例子

DB::table('test')
    ->orderByRaw("CAST(title as UNSIGNED) ASC")
    ->get();

回答by Saroj Shrestha

For Laravel Collection:

对于 Laravel 集合:

$collection = collect([
    ['sn' => '2'],
    ['sn' => 'B'],
    ['sn' => '1'],
    ['sn' => '10'],
    ['sn' => 'A'],
    ['sn' => '13'],
]);

$sorted = $collection->sortBy('sn');

$sorted = $collection->sortBy('sn');

//print_r($collection);

Illuminate\Support\Collection Object
(
    [items:protected] => Array
        (
            [2] => Array
                (
                    [sn] => 1
                )

            [0] => Array
                (
                    [sn] => 2
                )

            [3] => Array
                (
                    [sn] => 10
                )

            [5] => Array
                (
                    [sn] => 13
                )

            [4] => Array
                (
                    [sn] => A
                )

            [1] => Array
                (
                    [sn] => B
                )

        )

)

As you can see, this will sort it correctly. However, if you want to sort it and reindex it then,

如您所见,这将正确排序。但是,如果您想对其进行排序并重新编制索引,

$sorted = $collection->sortBy('sn')->values()->all();

$sorted = $collection->sortBy('sn')->values()->all();

//print_r($sorted)

Array
(
    [0] => Array
        (
            [sn] => 1
        )

    [1] => Array
        (
            [sn] => 2
        )

    [2] => Array
        (
            [sn] => 10
        )

    [3] => Array
        (
            [sn] => 13
        )

    [4] => Array
        (
            [sn] => A
        )

    [5] => Array
        (
            [sn] => B
        )

)

Furthermore, You can also pass your own callback to determine how to sort the collection values.

此外,您还可以通过自己的回调来确定如何对集合值进行排序。

$sorted = $collection->sortBy(function ($item, $key) {
    //your logic
});

For more details: https://laravel.com/docs/5.8/collections#method-sortby

更多详情:https: //laravel.com/docs/5.8/collections#method-sortby

回答by Che Safwan

DB::table('test')->orderByRaw('LENGTH(title)', 'ASC')
->orderBy('title', 'ASC')
->get();