'Laravel/ PHP: Order By Alphabetical with numbers in order [duplicate]
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.
Solution 1:[1]
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 MySQL but you could also take the results from your Laravel helper into array format and implement PHP's natsort
function instead.
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 orderBy
function. I'm only transcribing from the references I used in combination with your example too - I cannot guarantee the efficacy.
Solution 2:[2]
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/
Query
SELECT * FROM <table> ORDER BY CAST(<column> AS unsigned)
Example for laravel
DB::table('test')
->orderByRaw("CAST(title as UNSIGNED) ASC")
->get();
Solution 3:[3]
For Laravel this also works:
$collection = $collection->sortBy('order', SORT_REGULAR, true);
Solution 4:[4]
DB::table('test')->orderByRaw('LENGTH(title)', 'ASC')
->orderBy('title', 'ASC')
->get();
Solution 5:[5]
For Laravel Collection:
$collection = collect([
['sn' => '2'],
['sn' => 'B'],
['sn' => '1'],
['sn' => '10'],
['sn' => 'A'],
['sn' => '13'],
]);
$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();
//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
Solution 6:[6]
Order the resulting collection
$unorderedThings = Thing::orderBy('id')->get();
$orderedThings=$unorderedThings->sort();
Solution 7:[7]
This work's for me using eloquent, is very simple:
Eloquent:
$tests = Test::all();
$tests = $tests->sortBy('title', SORT_REGULAR, false); // false=ascending, true=descending
Sort numbers as text in Laravel.
I hope this is very helpful
Solution 8:[8]
Basically like the accepted answer but with the comma removed and orderByRaw. Otherwise I get an error about bindings
DB::table('test')->orderByRaw('LENGTH(title) ASC') ->orderBy('title', 'ASC') ->get();
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | Community |
Solution 2 | Joesel Duazo |
Solution 3 | Erhnam |
Solution 4 | Che Safwan |
Solution 5 | |
Solution 6 | Gerrlt |
Solution 7 | Radames E. Hernandez |
Solution 8 | MandyF |