itemsapi / itemsjs

Extremely fast faceted search engine in JavaScript - lightweight, flexible, and simple to use
Apache License 2.0
350 stars 41 forks source link

Case-insensitive sorting or custom sort #80

Closed BrunoVernay closed 3 years ago

BrunoVernay commented 3 years ago

Is there an option to set so that sorting is case-insensitive? Or is there a way to define custom search where one could implement case-insensitive sort? Thanks

cigolpl commented 3 years ago

Yes, you can define custom search and its sorting. See Integrations section in Readme

cigolpl commented 3 years ago

@BrunoVernay right now as a quick workaround I'd suggest you creating a copy of the field and make toLowerCase() on it and then sort on that new field.

BrunoVernay commented 3 years ago

Thanks. I ended up adding a static sort. My use case does not require to change it. So it is OK and simple. But I did read the doc and links you gave, I tried some stuff, but did not understand how to do it. I still think an example would be nice. Here what I have done for the static sort (near the end of search.js):

...
        result.data.items.sort( function(a, b) {
          var nameA = a.name.toUpperCase(); 
          var nameB = b.name.toUpperCase(); 
          if (nameA < nameB) {
            return -1;
          }
          if (nameA > nameB) {
            return 1;
          }     
          return 0;
        })

        return result
      }
    }
  });

It works for me.