TarekRaafat / autoComplete.js

Simple autocomplete pure vanilla Javascript library.
https://tarekraafat.github.io/autoComplete.js
Apache License 2.0
3.95k stars 240 forks source link

Why was sort removed? #200

Closed folknor closed 3 years ago

folknor commented 3 years ago

In https://github.com/TarekRaafat/autoComplete.js/commit/6d04d41dbcf539cb03c52adf67671a8794f8f50e you removed sort, and I can't find an open or closed issue or discussion that explains why.

Also the commit message has no information on why.

We use autocomplete to for example complete street addresses including house number, and we don't control the REST server and it returns results in an order we can't determine.

In autocomplete 8.x we just used sort, what should we do now?

Should we just sort before we return from data:src?

TarekRaafat commented 3 years ago

Hello @folknor,

sort API was removed because it became redundant eventually due to the existence of data.results which is much more flexible than sort because it covers a wide range of different use cases including sorting.

So, don't worry you could easily achieve the exact same sorting results as before even with better performance using the data.results API as used in the demo code for filtering duplicate values.

All you have to do is just moving your sorting algorithm inside data.results and apply the needed changes.

Please try it and let me how it goes.

Have a nice day! :)

folknor commented 3 years ago

Thanks I will try it tomorrow :-) Perhaps point to data.results in the changelog text.

TarekRaafat commented 3 years ago
data: {
  results: (list) => {
    return list.sort((a, b) => {
      if (a.match < b.match) return -1;
      if (a.match > b.match) return 1;
      return 0;
    })
  }
}