How to sort incoming data by EnginioModel



  • I want to sort some data by EnginioModel.
    The Enginio dashboard has a button:
    !http://i1288.photobucket.com/albums/b485/jiangcaiyang/1_zpsa9fd41ba.png(sort button)!
    I want to know how to do it using EnginioModel.



  • You can add a "sort option in the queries":https://developer.qtcloudservices.com/eds/key-concepts/queries



  • Thank you. I'll have a try.



  • Gadlim is right in 80% :-)

    Yes, EnginioModel supports "sort", but it is an initial sort only, which means that appended/modified values will not be automatically sorted. To have that functionality you need to use QSortFilterProxyModel (http://qt-project.org/doc/qt-5/QSortFilterProxyModel.html)



  • Thank you all.
    In my use case, I need to sort a bunch of data in different priorities, in other words, I need a primary sort and a secondary sort, etc.

    I am not using EnginioModel as my model, instead, I use ListModel with only id field to get access to, and I define some sort functions like this:
    @function elapsedDesc( a, b )
    {
    if ( a.elapsed === b.elapsed )// 耗时
    {
    if ( a.level === b.level )// 关卡
    {
    if ( a.difficulty === b.difficulty )// 难度
    {
    if ( a.score > b.score ) return -1;// 分数
    else return 1;
    }
    else if ( a.difficulty > b.difficulty ) return -1;
    else return 1;
    }
    else if ( a.level > b.level ) return -1;
    else return 1;
    }
    else if ( a.elapsed > b.elapsed ) return -1;
    else return 1;
    }@

    when Enginio reply is coming, I use the following code to organize my data:

    @function sort( sortFunc )
    {
    if ( theResult !== undefined )
    {
    enginioModel.clear( );
    theResult.sort( sortFunc );
    for ( var i in theResult )
    {
    enginioModel.append( theResult[i] );
    }
    }
    }

    onFinished:
    {
    console.log( "Got the result!" );
    theResult = reply.data.results;
    sort( elapsedDesc );
    }@

    it meet the demand with no problem.
    At last, I am using QML only, not want to mix up with C++ code.

    [quote author="Nierob" date="1400746805"]Gadlim is right in 80% :-)

    Yes, EnginioModel supports "sort", but it is an initial sort only, which means that appended/modified values will not be automatically sorted. To have that functionality you need to use QSortFilterProxyModel (http://qt-project.org/doc/qt-5/QSortFilterProxyModel.html)[/quote]

    [quote author="gadlim" date="1400682517"]You can add a "sort option in the queries":https://developer.qtcloudservices.com/eds/key-concepts/queries[/quote]



  • I hope that you do not have too many items, the approach you use, is a bit inefficient. Doesn't it flicker on sorting?

    As a side note I would like to know why you decided to not use EnginioModel?

    ps. you can use QSortFilterProxyModel in QML too, the only thing you need is to register it as a QML type in for example main.cpp



  • Here the original copy of data of reply has been handled by "theResult" variable. Thus when client click a sort button, it sort immediately without fetching data from Enginio again. Of course when the data is quite huge, it may flicker, but I don't try it, anyway. After all the Enginio reply is slow sometimes and may not receive any data at all.
    [quote author="Nierob" date="1400750335"]I hope that you do not have too many items, the approach you use, is a bit inefficient. Doesn't it flicker on sorting?

    As a side note I would like to know why you decided to not use EnginioModel?

    ps. you can use QSortFilterProxyModel in QML too, the only thing you need is to register it as a QML type in for example main.cpp

    [/quote]



  • I meant@function sort( sortFunc )@ It clears the model and then iteratively it adds new items. On each append a view needs to be updated. Anyway it is small issue, and if the current solution works for you then it is fine :-)


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.