Anup
Anup

Reputation: 221

Selectively sorting a store in sencha

I want to sort a store in Sencha selectively.

Scenario: Say, there are 5 elements in my store as follows.
1. Ad
2. Ab
3. Ac
4. Af
5. Ae

So, when I display these records in a list, I want to keep "Ac" always at the 0th position(1st record) and other 4 sorted like below:
1. Ac
2. Ab
3. Ad
4. Ae
5. Af

Sorting the store like below,
sorters:{
property: 'record',
direction: 'ASC'
},

will give regular sorting result as follows:
1. Ab
2. Ac
3. Ad
4. Ae
5. Af

Appreciate if someone can help. Thanks.

Upvotes: 1

Views: 1883

Answers (2)

rixo
rixo

Reputation: 25001

You can use sorterFn instead of property, to sort using a custom function.

In your example, that would give the desired result:

{
    direction: 'ASC'
    ,sorterFn: function(left, right) {
        var l = left.get('record') || '',
            r = right.get('record') || '';

        // Special case 'Ac' is always first
        if (l === 'Ac') {
            if (r === 'Ac') {
                return 0;
            } else {
                return -1;
            }
        } else if (r === 'Ac') {
            return 1;
        }

        // Defaults to a standard string comparison
        else {
            return l.localeCompare(r);
        }
    }
}

Upvotes: 2

Anup
Anup

Reputation: 221


In above scenario, I was actually adding "Ac" from client side and getting the other records from server. So, instead of sorting my store using Sencha sorter, the way I did it as follows.
1. Modify the query which gets data from server as "select xxx, xxx, record from xxxxx ordered by record"
2. Add "Ac" at the 0th position in the store.
store.add(0,{record:"Ac"})

Though I completely agree, that it's one of the scenario and not the answer to the question I asked.

Upvotes: 0

Related Questions