From a3cc8cc5d8241576d8355200ef79854aedb43c2f Mon Sep 17 00:00:00 2001 From: Henry Jameson Date: Sat, 8 Jun 2019 17:15:48 +0300 Subject: [PATCH] sorting --- src/components/emoji-input/suggestor.js | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/src/components/emoji-input/suggestor.js b/src/components/emoji-input/suggestor.js index 54fd7f29..c414b1bf 100644 --- a/src/components/emoji-input/suggestor.js +++ b/src/components/emoji-input/suggestor.js @@ -29,7 +29,21 @@ function suggestUsers (users) { user.screen_name.toLowerCase().startsWith(noPrefix) || user.name.toLowerCase().startsWith(noPrefix) /* eslint-disable camelcase */ - ).map(({ screen_name, name, profile_image_url_original }) => ({ + ).slice(0, 20).sort((a, b) => { + let aScore = 0 + let bScore = 0 + + aScore += a.screen_name.toLowerCase().startsWith(noPrefix) * 2 + aScore += a.name.toLowerCase().startsWith(noPrefix) + bScore += b.screen_name.toLowerCase().startsWith(noPrefix) * 2 + bScore += b.name.toLowerCase().startsWith(noPrefix) + + const diff = bScore * 10 - aScore * 10 + const nameAlphabetically = a.name > b.name ? 1 : -1 + const screenNameAlphabetically = a.screen_name > b.screen_name ? 1 : -1 + + return diff + nameAlphabetically + screenNameAlphabetically + }).map(({ screen_name, name, profile_image_url_original }) => ({ displayText: screen_name, detailText: name, imageUrl: profile_image_url_original,