View Full Version : sortInfo Sort by multiple fields

20 Jul 2009, 11:52 AM
Is there a way to sort by multiple fields. For example if I have an editable grid with travel destination, from, and to dates where the entire record can be the same including from and only to differs. I would like to sort by destination, from, and to.

Here is an example how the data should be sorted.

Florida, 1/1/2009, 1/10/2009
Florida, 3/1/2009, 4/1/2009
Florida, 3/1/2009, 5/1/2009
Las Vegas, 5/1/2009, 6/1/2009

20 Jul 2009, 11:57 AM
I'm not quite sure if Ext can handle this, but I would try sorting it on the server side. Let the database do the hard work ;)

20 Jul 2009, 1:40 PM
Sorry, my fault for not specifying, the grid will be part of a form to submit data and the data is not yet in the database. It will only be applied to the database once they enter it and press save or via an ajax call.

21 Jul 2009, 3:31 AM
It depends.

If you click on the 'from' column, would the sort order depend on the previous sort or would the sort order by fixed ('from', 'destination', 'to')?

For the first one you would have to modify the internal Store code.

For the second one you would specify your own sortType function for every field to do custom sorting.

21 Jul 2009, 7:26 AM
The second one is what i'm trying to do. Any examples to look at?

21 Jul 2009, 9:35 AM
I forgot, you still need to patch Store (but it's part of my default overrides):

Ext.override(Ext.data.Store, {
sortData : function(f, direction){
direction = direction || 'ASC';
var st = this.fields.get(f).sortType;
var fn = function(r1, r2){
var v1 = st(r1.data[f], r1), v2 = st(r2.data[f], r2);
return v1 > v2 ? 1 : (v1 < v2 ? -1 : 0);
this.data.sort(direction, fn);
if(this.snapshot && this.snapshot != this.data){
this.snapshot.sort(direction, fn);

After including this override you can use:

fields: [
{name: 'destination', sortType: function(v, r){
return String.leftPad(r.get('destination'), 20).substr(0, 20) +
r.get('from').dateFormat('YmdHis') +
{name: 'from', sortType: function(v, r){
return r.get('from').dateFormat('YmdHis') +
String.leftPad(r.get('destination'), 20).substr(0, 20) +
{name: 'to', sortType: function(v, r){
return r.get('to').dateFormat('YmdHis');
String.leftPad(r.get('destination'), 20).substr(0, 20) +
r.get('from').dateFormat('YmdHis') +

22 Jul 2009, 7:03 AM
awesome, thanks.

18 Feb 2010, 10:54 PM
Cool, I just find this solution. You saved my day. It works great. Thanks!