Welcome!

Welcome to the official BlackBerry Support Community Forums.

This is your resource to discuss support topics with your peers, and learn from each other.

inside custom component

Native Development

Reply
New Developer
Loomist
Posts: 25
Registered: ‎02-14-2012
My Device: PlayBook / Dev Alpha

GroupDataModel & sortingKeys: unable to sort by integer?

[ Edited ]

I'm running into a frustrating issue and I'm hoping someone else out there has found a way around it.  If I have a GroupDataModel and the sorting keys are sorting numbers, such as 25, 30, 40, 100, 150, 200, 250, 300, 350, etc, the list is showing those keys sorted in this order:

100

150

200

25

250

30

300

350

40

 

Is there any possible way to sort these not by their first digit, but as an integer, so it would sort in proper order?

 

Thanks!


:wq!
Contributor
TheStoof
Posts: 20
Registered: ‎02-21-2012
My Device: Bold 9900

Re: GroupDataModel & sortingKeys: unable to sort by integer?

Let's say this is your code:

 

attachedObjects: [
        // Custom data source object, loads data using a SqlConnection, see customsqldatasource.cpp
        GroupDataModel {
            id: dataModel
            sortingKeys: [
                "datePlayed",
                "player1",
                "player2"
            ]
            sortedAscending: true
            grouping: ItemGrouping.ByFullValue
        },
        CustomSqlDataSource {
            id: dataSource
            source: "sql/scores.sqlite"
            query: "SELECT rowid,* FROM scores"
            onDataLoaded: {
                dataModel.insertList(data);
                console.log(data.length);
            }
        }
    ]

 datePlayed, player1, player2 are KEYS, or column names.

 

if I add data:

 

var itemData = {
            "datePlayed": datePlayed,
            "player1": player1,
            "player2": player2
        }
        myListView.dataModel.insert(itemData);

 You can clearly see that I've typed "datePlayed": which means "key": value.

 

Now, in the first block of code, I'm telling it to sort by date, THEN by player1, THEN by player2, and of course, group by date because I believe that's first.

Developer
strobejb
Posts: 282
Registered: ‎10-15-2012
My Device: bb10 developer

Re: GroupDataModel & sortingKeys: unable to sort by integer?

The simplest method is just to add an extra property to your data items. Keep the one you currently have for the display (where it will print "100", "20" etc) - and reference that as usual in your listItemComponent. But now add an extra property for sorting, where you either store the values as an integer (or a QVariant(int) or whatever)... or prefix all the numbers with "0000"s:

 

0000100

0000150

0000200

0000020

 

etc

Contributor
TheStoof
Posts: 20
Registered: ‎02-21-2012
My Device: Bold 9900

Re: GroupDataModel & sortingKeys: unable to sort by integer?

I didn't even see this.

You can change "by first character" to "grouping: ItemGrouping.ByFullValue". Try that?
New Developer
Loomist
Posts: 25
Registered: ‎02-14-2012
My Device: PlayBook / Dev Alpha

Re: GroupDataModel & sortingKeys: unable to sort by integer?

I should have mentioned this in my original post, but this is with an XML data model.  And unfortunately, it's coming from another source, so the XML is already formatted without the preceding 0's.  If I could add preceding 0's, it would definitely work (as I verified this), but unfortunately, cannot.  Thoughts?


:wq!
Developer
strobejb
Posts: 282
Registered: ‎10-15-2012
My Device: bb10 developer

Re: GroupDataModel & sortingKeys: unable to sort by integer?

In your original post you stated you were using a GroupDataModel?

 

Post your code of what you have at the moment

 

my thoughts:

 

1. Do what I suggested originally, because I know this works

2. Use an XmlDataModel if you have xml data

3. modify your xml after you load it, but before you put it into your datamodel