1

I am trying to sort some items (sort a map) , I can sort it successfully, but I want to exclude some items based on it's attribute

Right now I am sorting like this based on attribute - price

 return (product.attr('active') !== 'f'
                }).sort(function(pA,pB){
                  return pB.attr('price') - pA.attr('price'); });

I want to skip some items based on attr('product_id') so the listed product_id's wont be sorted based, and will be returned first.

return (product.attr('active') !== 'f'
      }).sort(function(pA,pB){
   return pB.attr('price') - pA.attr('price'); }).except(pA.attr('product_id') == 5677));

Something like above, obviously except function does not exist.

Is there a way to exclude some items from sorting, based on it's attributes like id?

Data

Map
active
:
true
brand_id
:
1
categories
:
Map(2) ["All Products", "Snacks", _cid: ".map232", _computedAttrs: {…}, __bindEvents: {…}, _comparatorBound: false, _bubbleBindings: {…}, …]
channel_id
:
1
created
:
"2017-08-14T19:16:56.148029-07:00"
description
:
"Breakfast"
image
:
"/media/333807.png"
name
:
"Breakfast"
price
:
"1"
product_id
:
5677
Cowgirl
  • 1,454
  • 5
  • 19
  • 40

1 Answers1

3

You can sort the wanted item to front by using a check and return the delta of the check.

var array = [{ product_id: 1, price: 1 }, { product_id: 2, price: 3 }, { product_id: 3, price: 4 }, { product_id: 4, price: 1 }, { product_id: 5, price: 8 }, { product_id: 5677, price: 1 }];

array.sort(function (a, b) {
    return (b.product_id === 5677) - (a.product_id === 5677) || b.price - a.price;
});

console.log(array);
.as-console-wrapper { max-height: 100% !important; top: 0; }

With more than just one id for sorting to top

var array = [{ product_id: 1, price: 1 }, { product_id: 2, price: 3 }, { product_id: 3, price: 4 }, { product_id: 4, price: 1 }, { product_id: 5, price: 8 }, { product_id: 5677, price: 1 }];
    topIds = [5677, 2]

array.sort(function (a, b) {
    return topIds.includes(b.product_id) - topIds.includes(a.product_id) || b.price - a.price;
});

console.log(array);
.as-console-wrapper { max-height: 100% !important; top: 0; }
Nina Scholz
  • 376,160
  • 25
  • 347
  • 392
  • How can I make it work for multiple product_id's? I need to exclude more than one `product_id` – Cowgirl Feb 22 '18 at 19:47
  • do the top items need a special sorting, despite of the standard descending sorting by `price`? – Nina Scholz Feb 22 '18 at 19:52
  • No, top items do not need a special sorting. Will this work with other stand descending sorting by name, date and other sorting methods? – Cowgirl Feb 22 '18 at 19:54
  • you could chain the other sorting criteria with logical OR behind the price sorting. – Nina Scholz Feb 22 '18 at 19:57
  • It excludes those items for sorting, for sure, but it's being returned last, I need to return it first – Cowgirl Feb 22 '18 at 20:33
  • then just switch `a` and `b`. – Nina Scholz Feb 22 '18 at 20:36
  • Thank you so much, everything works, except, what is the best way to work this when comparing names `if (nameA < nameB) return -1; if(nameA > nameB) return 1; return 0; });` – Cowgirl Feb 22 '18 at 20:47
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/165682/discussion-between-nina-scholz-and-cowgirl). – Nina Scholz Feb 22 '18 at 20:49