Browse Source

Fixes #4444 - Incorrectly sorts the list of agents when the name contains diacritics

Mantas 1 year ago
parent
commit
2afbf807f0

+ 35 - 24
app/assets/javascripts/app/models/_application_model.coffee

@@ -759,33 +759,44 @@ set new attributes of model (remove already available attributes)
 
     all_complied
 
-  @_sortBy: (collection, attribute, translate) ->
-    _.sortBy(collection, (item) ->
-
-      # set displayName as default sort attribute
-      if !attribute
-        attribute = 'displayName'
-
-      # check if displayName exists
-      if attribute is 'displayName'
-        if item.displayName
-          value = item.displayName()
-          valueProcessed = if translate then App.i18n.translateInline(value) else value
-          return valueProcessed.toLowerCase()
-        else
-          attribute = 'name'
-
-      return '' if item[ attribute ] is undefined
-      return '' if item[ attribute ] is null
-
-      # return value if string
-      if item[ attribute ].toLowerCase
-        value = item[ attribute ]
+  @_sortByItem: (item, attribute, translate) ->
+    # set displayName as default sort attribute
+    if !attribute
+      attribute = 'displayName'
+
+    # check if displayName exists
+    if attribute is 'displayName'
+      if item.displayName
+        value = item.displayName()
         valueProcessed = if translate then App.i18n.translateInline(value) else value
         return valueProcessed.toLowerCase()
+      else
+        attribute = 'name'
 
-      item[ attribute ]
-    )
+    return '' if item[ attribute ] is undefined
+    return '' if item[ attribute ] is null
+
+    # return value if string
+    if item[ attribute ].toLowerCase
+      value = item[ attribute ]
+      valueProcessed = if translate then App.i18n.translateInline(value) else value
+      return valueProcessed.toLowerCase()
+
+    item[ attribute ]
+
+  @_sortBy: (collection, attribute, translate) ->
+    collection.sort (a,b) =>
+      aValue = @_sortByItem(a, attribute, translate)
+      bValue = @_sortByItem(b, attribute, translate)
+
+      return aValue.localeCompare(bValue) if aValue.localeCompare
+
+      if aValue > bValue || aValue is null
+        1
+      else if aValue < bValue || bValue is null
+        -1
+      else
+        0
 
   @_order: (collection, attribute) ->
     if attribute is 'DESC'

+ 39 - 1
public/assets/tests/qunit/model.js

@@ -32,7 +32,8 @@ QUnit.test( "TicketPriority search tests", assert => {
       active:     true,
       created_at: '2014-06-10T10:17:54.000Z',
     },
-  ] )
+  ], { clear: true } )
+
   priorities = App.TicketPriority.search({sortBy:'created_at', order: 'ASC'})
   assert.equal('2 normal', priorities[0].name, 'check 1 entry')
   assert.equal('3 high', priorities[1].name, 'check 2 entry')
@@ -52,6 +53,43 @@ QUnit.test( "TicketPriority search tests", assert => {
   assert.equal(undefined, priorities[1], 'check name filter is undefined')
 });
 
+// Search results sorting with accents
+QUnit.test( "Search results sorting with accents", assert => {
+  App.TicketPriority.refresh( [
+    {
+      id:         2,
+      name:       'B priority',
+      note:       'some note 2',
+      active:     false,
+      created_at: '2014-06-10T10:17:33.000Z',
+    },
+    {
+      id:         1,
+      name:       'Ą priority',
+      note:       'some note 1',
+      active:     true,
+      created_at: '2014-06-10T11:17:34.000Z',
+    },
+    {
+      id:         3,
+      name:       'C priority',
+      note:       'some note 3',
+      active:     true,
+      created_at: '2014-06-10T10:17:44.000Z',
+    },
+  ], { clear: true } )
+
+  priorities = App.TicketPriority.search({sortBy:'name', order: 'ASC'})
+  assert.equal('Ą priority', priorities[0].name, 'check 1 entry')
+  assert.equal('B priority', priorities[1].name, 'check 2 entry')
+  assert.equal('C priority', priorities[2].name, 'check 3 entry')
+
+  priorities = App.TicketPriority.search({sortBy:'name', order: 'DESC'})
+  assert.equal('C priority', priorities[0].name, 'check 1 entry')
+  assert.equal('B priority', priorities[1].name, 'check 2 entry')
+  assert.equal('Ą priority', priorities[2].name, 'check 3 entry')
+});
+
 // PublicLink search
 QUnit.test( "PublicLink search tests", assert => {