Skip to content
Permalink
Browse files

[FEATURE][expression] Brand new array_sort() function

  • Loading branch information
nirvn committed Dec 20, 2018
1 parent d10eb89 commit e9e1e0d6f35edc5255b19d2eb4c97ca30c37d701
@@ -0,0 +1,8 @@
{
"name": "array_sort",
"type": "function",
"description": "Returns the provided array with its elements sorted.",
"arguments": [ {"arg":"array","description":"an array"},
{"arg":"ascending","optional":true,"default":"true","description":"set this parameter to false to sort the array in descending order"}],
"examples": [ { "expression":"array_sort(array(3,2,1))", "returns":"[ 1, 2, 3 ]"}]
}
@@ -4142,6 +4142,14 @@ static QVariant fcnArray( const QVariantList &values, const QgsExpressionContext
return values;
}

static QVariant fcnArraySort( const QVariantList &values, const QgsExpressionContext *, QgsExpression *parent, const QgsExpressionNodeFunction * )
{
QVariantList list = QgsExpressionUtils::getListValue( values.at( 0 ), parent );
bool ascending = values.value( 1 ).toBool();
std::sort( list.begin(), list.end(), [ascending]( QVariant a, QVariant b ) -> bool { return ( !ascending ? qgsVariantLessThan( b, a ) : qgsVariantLessThan( a, b ) ); } );
return list;
}

static QVariant fcnArrayLength( const QVariantList &values, const QgsExpressionContext *, QgsExpression *parent, const QgsExpressionNodeFunction * )
{
return QgsExpressionUtils::getListValue( values.at( 0 ), parent ).length();
@@ -5015,6 +5023,7 @@ const QList<QgsExpressionFunction *> &QgsExpression::Functions()
<< new QgsArrayForeachExpressionFunction()
<< new QgsArrayFilterExpressionFunction()
<< new QgsStaticExpressionFunction( QStringLiteral( "array" ), -1, fcnArray, QStringLiteral( "Arrays" ), QString(), false, QSet<QString>(), false, QStringList(), true )
<< new QgsStaticExpressionFunction( QStringLiteral( "array_sort" ), QgsExpressionFunction::ParameterList() << QgsExpressionFunction::Parameter( QStringLiteral( "array" ) ) << QgsExpressionFunction::Parameter( QStringLiteral( "ascending" ), true, true ), fcnArraySort, QStringLiteral( "Arrays" ) )
<< new QgsStaticExpressionFunction( QStringLiteral( "array_length" ), 1, fcnArrayLength, QStringLiteral( "Arrays" ) )
<< new QgsStaticExpressionFunction( QStringLiteral( "array_contains" ), QgsExpressionFunction::ParameterList() << QgsExpressionFunction::Parameter( QStringLiteral( "array" ) ) << QgsExpressionFunction::Parameter( QStringLiteral( "value" ) ), fcnArrayContains, QStringLiteral( "Arrays" ) )
<< new QgsStaticExpressionFunction( QStringLiteral( "array_find" ), QgsExpressionFunction::ParameterList() << QgsExpressionFunction::Parameter( QStringLiteral( "array" ) ) << QgsExpressionFunction::Parameter( QStringLiteral( "value" ) ), fcnArrayFind, QStringLiteral( "Arrays" ) )
@@ -2723,6 +2723,9 @@ class TestQgsExpression: public QObject
QCOMPARE( QgsExpression( "array_slice(array('Dufour','Valmiera','Chugiak','Brighton'),1,2) = array('Valmiera','Chugiak')" ).evaluate( &context ), QVariant( true ) );
QCOMPARE( QgsExpression( "array_slice(array('Dufour','Valmiera','Chugiak','Brighton'),-2,-1) = array('Chugiak','Brighton')" ).evaluate( &context ), QVariant( true ) );
QCOMPARE( QgsExpression( "array_slice( array(), 0, 3) = array()" ).evaluate( &context ), QVariant( true ) );

QCOMPARE( QgsExpression( "array_sort(array('Banana','Cake','Apple'))" ).evaluate( & context ), QVariant( QStringList() << QStringLiteral( "Apple" ) << QStringLiteral( "Banana" ) << QStringLiteral( "Cake" ) ) );
QCOMPARE( QgsExpression( "array_sort(array('Banana','Cake','Apple'),false)" ).evaluate( & context ), QVariant( QStringList() << QStringLiteral( "Cake" ) << QStringLiteral( "Banana" ) << QStringLiteral( "Apple" ) ) );
}

void eval_int_array()
@@ -2794,6 +2797,9 @@ class TestQgsExpression: public QObject
QCOMPARE( QgsExpression( "array_slice(array(1,2,3,4,5),-2,-1) = array(4,5)" ).evaluate( &context ), QVariant( true ) );
QCOMPARE( QgsExpression( "array_slice(array(1,2,3,4,5),-1,-1) = array(5)" ).evaluate( &context ), QVariant( true ) );

QCOMPARE( QgsExpression( "array_sort(array(1,10,2,30,4))" ).evaluate( &context ), QVariant( QVariantList() << 1 << 2 << 4 << 10 << 30 ) );
QCOMPARE( QgsExpression( "array_sort(array(1,10,2,30,4),false)" ).evaluate( &context ), QVariant( QVariantList() << 30 << 10 << 4 << 2 << 1 ) );

QVariantList foreachExpected;
foreachExpected << 10 << 20 << 40;
QCOMPARE( QgsExpression( "array_foreach(array(1, 2, 4), @element * 10)" ).evaluate( &context ), QVariant( foreachExpected ) );

0 comments on commit e9e1e0d

Please sign in to comment.
You can’t perform that action at this time.