-
7 Feb 2013 10:37 PM #1
Answered: How to sort case-insensitive when using addSortInfo
Answered: How to sort case-insensitive when using addSortInfo
I would like to sort my treestore so I tried the following. The problem was it sorted case-sensitive so how can I sort it case-insensitive?
store.addSortInfo(new StoreSortInfo<BaseDto>(props.name(), SortDir.ASC));
-
Best Answer Posted by Colin Alworth
The default uses the standard Comparable.compareTo sort, which for String (your property is a String) is case sensitive. StoreSortInfo has other constructors, including one that takes a Comparator, allowing you to specify how you want the sort to be performed:
Code:store.addSortInfo(new StoreSortInfo<BaseDto>(props.name(), new CaseInsensitiveComparator(), SortDir.ASC)) //... public class CaseInsensitiveComparator implements Comparator<String>() { public int compare(String o1, String o2) { //implement logic here - here's what it could look like: return o1.toLowerCase().compareTo(o2.toLowerCase()); } }
-
8 Feb 2013 10:20 AM #2
The default uses the standard Comparable.compareTo sort, which for String (your property is a String) is case sensitive. StoreSortInfo has other constructors, including one that takes a Comparator, allowing you to specify how you want the sort to be performed:
Code:store.addSortInfo(new StoreSortInfo<BaseDto>(props.name(), new CaseInsensitiveComparator(), SortDir.ASC)) //... public class CaseInsensitiveComparator implements Comparator<String>() { public int compare(String o1, String o2) { //implement logic here - here's what it could look like: return o1.toLowerCase().compareTo(o2.toLowerCase()); } }
-
8 Feb 2013 1:20 PM #3
-
2 May 2013 6:19 AM #4
Another way
Another way
I had the similiar issue and i'm new to sencha, I searched around fro a few hours and tried several things. Frustrated I decided just to change the Ext.Data.Proxy.SQL.JS file. I'm my opinion, I think all string comparision should be case insensative by default.
Changes I made are in red.
selectRecords: function(transaction, params, callback, scope) { var me = this, table = me.getTable(), idProperty = me.getModel().getIdProperty(), sql = 'SELECT * FROM ' + table, records = [],
filterStatement = ' WHERE ',
sortStatement = ' ORDER BY UPPER(',
i, ln, data, result, count, rows, filter, sorter, property, value; result = new Ext.data.ResultSet({ records: records, success: true }); if (!Ext.isObject(params)) { sql += filterStatement + idProperty + ' = ' + params; } else { ln = params.filters && params.filters.length; if (ln) { for (i = 0; i < ln; i++) { filter = params.filters[i]; property = filter.getProperty(); value = filter.getValue(); if (property !== null) { sql += filterStatement + property + ' ' + (filter.getAnyMatch() ? ('LIKE \'%' + value + '%\'') : ('= \'' + value + '\'')); filterStatement = ' AND '; } } } ln = params.sorters && params.sorters.length; if (ln) { for (i = 0; i < ln; i++) { sorter = params.sorters[i]; property = sorter.getProperty(); if (property !== null) {
sql += sortStatement + property + ') ' + sorter.getDirection();
sortStatement = ', '; } } } // handle start, limit, sort, filter and group params if (params.page !== undefined) { sql += ' LIMIT ' + parseInt(params.start, 10) + ', ' + parseInt(params.limit, 10); } } transaction.executeSql(sql, null, function(transaction, resultSet) { rows = resultSet.rows; count = rows.length; for (i = 0, ln = count; i < ln; i++) { data = rows.item(i); records.push({ clientId: null, id: data[idProperty], data: data, node: data }); } result.setSuccess(true); result.setTotal(count); result.setCount(count); if (typeof callback == 'function') { callback.call(scope || me, result); } }, function(transaction, errors) { result.setSuccess(false); result.setTotal(0); result.setCount(0); if (typeof callback == 'function') { callback.call(scope || me, result); } } ); },Last edited by mpavlis; 2 May 2013 at 6:20 AM. Reason: The format was destroyed after save


Reply With Quote