javascript - JS sort array by three types of sorting - Stack Overflow

I need to sort an array by the following order based on a search term.Exact string.Starts with. Conta

I need to sort an array by the following order based on a search term.

  1. Exact string.
  2. Starts with.
  3. Contains.

Code :

var arr = ['Something here Hello', 'Hell', 'Hello'];

var term = 'Hello';

var sorted = arr.slice().sort((a, b) => {
  let value = 0;

  if (a.startsWith(term)) {
    value = -1;
  }

  if (a.indexOf(term) > -1) {
    value = -1;
  }

  if (a === term) {
    value = -1;
  }

  return value;
});

console.log(sorted);

I need to sort an array by the following order based on a search term.

  1. Exact string.
  2. Starts with.
  3. Contains.

Code :

var arr = ['Something here Hello', 'Hell', 'Hello'];

var term = 'Hello';

var sorted = arr.slice().sort((a, b) => {
  let value = 0;

  if (a.startsWith(term)) {
    value = -1;
  }

  if (a.indexOf(term) > -1) {
    value = -1;
  }

  if (a === term) {
    value = -1;
  }

  return value;
});

console.log(sorted);

The expected result is:

["Hello", "Hell", "Something here Hello"]

I'm not sure how to do this with the built-in sort function because it looks like it's not meant to use with cases like that. Any advice, please?

Share Improve this question edited Jan 17, 2019 at 13:12 Nina Scholz 387k26 gold badges364 silver badges414 bronze badges asked Jan 17, 2019 at 12:33 undefinedundefined 6,91413 gold badges53 silver badges102 bronze badges 3
  • You have to pare a and b. Your current algorithm just returns -1 or 0 for all your array elements. – str Commented Jan 17, 2019 at 12:40
  • because it looks like it's not meant to use with cases like that it looks like a bigger problem (and maybe the core of your problem) is that your sorting function is returning results that are not logically equivalent! You should be returning -1, 0 but also 1. Right now you only return one of two values - two objects are equal or smaller. It's functionally equivalent to returning a boolean as you still only have two outes. – VLAZ Commented Jan 17, 2019 at 12:40
  • @str it also returns 0 if the term is nowhere to be found. Not that it makes it better. – VLAZ Commented Jan 17, 2019 at 12:41
Add a ment  | 

1 Answer 1

Reset to default 9

You need a function which returns a value for the staged sorting.

Inside of the callback for sorting, you need to return the delta of the two values which reflects the relation between the two strings.

const pareWith = term => string => {
        if (string === term) return 1;
        if (term.startsWith(string)) return 2; // switch string and term
        if (string.includes(term)) return 3;   // use includes
        return Infinity;                       // unknown strings move to the end
    };

var array = ['Something here Hello', 'Hell', 'Hello'],
    term = 'Hello',
    order = pareWith(term);

array.sort((a, b) => order(a) - order(b));

console.log(array);

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745401265a4626113.html

相关推荐

  • javascript - JS sort array by three types of sorting - Stack Overflow

    I need to sort an array by the following order based on a search term.Exact string.Starts with. Conta

    3小时前
    20

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信