0

I want to NatSort Eloquent model output. I have attributes like Size 1/2, Size 3/4, Weight 1, Weight 2, Weight 10...

In other words I want to natural sort strings but if there is fractions I want to sort them in ascending order too. (example: 3/8 is lower than 1/2)

CategoryAttribute::where('category_id', $catId)->with('attributes')->get();

I have position column in table and want to sort them and save as sorted positions. How can I make it the most performance friendly?

Buglinjo
  • 2,067
  • 1
  • 15
  • 26
  • http://stackoverflow.com/questions/153633/natural-sort-in-mysql – DevK Jan 23 '17 at 15:32
  • Actually you can just use [`$collection->sort('attributes')`](https://laravel.com/docs/5.3/collections#method-sort). I've tried it and it seems to perform a natural sort. Double check it to be sure. – DevK Jan 23 '17 at 15:36
  • Can it perform mathematical calculations of 3/8, 1/2, 5/8, 3/4? – Buglinjo Jan 23 '17 at 15:37
  • Nah not by default. I think your best bet would be to save fractions in a separate column. – DevK Jan 23 '17 at 16:18
  • But Many of them does not have fractions they have only numbers or only text. Only for fractions I don't think that it's a good idea to create column :/ – Buglinjo Jan 23 '17 at 16:19

0 Answers0