Answer the question
In order to leave comments, you need to log in
How to sort string field as integer?
Request for a selection of goods, sorted by capacity.
$products = Product()::find()->orderBy(['power' => SORT_ASC])->active()->all();
1000, 600, 750, 800, 900, 950
600, 750, 800, 900, 950, 1000
.
Answer the question
In order to leave comments, you need to log in
Used the mysql CAST function
$sort = new Sort([
'attributes' => [
'power' => [
'asc' => [new Expression('CAST(' . AttributeValue::tableName() . '.value as SIGNED) ASC')],
'desc' => [new Expression('CAST(' . AttributeValue::tableName() . '.value as SIGNED) DESC')],
'default' => SORT_DESC,
'label' => 'Мощность',
],
],
]);
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question